How do I add a tuple to a Swift Array?

后端 未结 8 490
夕颜
夕颜 2021-01-30 03:02

I\'m trying to add a tuple (e.g., 2-item tuple) to an array.

var myStringArray: (String,Int)[]? = nil
myStringArray += (\"One\", 1)

What I\'m g

相关标签:
8条回答
  • 2021-01-30 03:52

    Thanks to comments:

    import UIKit
    
    @assignment func += (inout left: Array<(String, Int)>?, right: (String, Int)) {
        if !left {
            left = []
        }
        if left {
            var array = left!
            array.append(right.0, right.1)
            left = array
        }
    }
    
    class ViewController: UIViewController {
        override func viewDidLoad() {
            super.viewDidLoad()
    
            let interestingNumbers = [
                "Prime": [2, 3, 5, 7, 11, 13],
                "Fibonacci": [1, 1, 2, 3, 5, 8],
                "Square": [1, 4, 9, 16, 25],
            ]
    
            println("interestingNumbers: \(interestingNumbers)\n")
            var largest = 0
    
            var myStringArray: (String,Int)[]? = nil
    
            myStringArray += ("One", 1)
    
            var x = 0
    
            for (kind, numbers) in interestingNumbers {
                println(kind)
                for number in numbers {
                    if number > largest {
                        largest = number
                    }
                    x++
                    println("\(x)) Number: \(number)")
                    myStringArray += (kind,number)
                } // end Number
            } // end Kind
            println("myStringArray: \(myStringArray)")
        }
    }
    

    The Output:

    interestingNumbers: [Square: [1, 4, 9, 16, 25], Prime: [2, 3, 5, 7, 11, 13], Fibonacci: [1, 1, 2, 3, 5, 8]]

    Square
    1) Number: 1
    2) Number: 4
    3) Number: 9
    4) Number: 16
    5) Number: 25
    Prime
    6) Number: 2
    7) Number: 3
    8) Number: 5
    9) Number: 7
    10) Number: 11
    11) Number: 13
    Fibonacci
    12) Number: 1
    13) Number: 1
    14) Number: 2
    15) Number: 3
    16) Number: 5
    17) Number: 8



    Array of tupules:

    myStringArray: [(One, 1), (Square, 1), (Square, 4), (Square, 9), (Square, 16), (Square, 25), (Prime, 2), (Prime, 3), (Prime, 5), (Prime, 7), (Prime, 11), (Prime, 13), (Fibonacci, 1), (Fibonacci, 1), (Fibonacci, 2), (Fibonacci, 3), (Fibonacci, 5), (Fibonacci, 8)]

    0 讨论(0)
  • 2021-01-30 03:53

    You have two issues. First problem, you're not creating an "array of tuples", you're creating an "optional array of tuples". To fix that, change this line:

    var myStringArray: (String,Int)[]? = nil
    

    to:

    var myStringArray: (String,Int)[]
    

    Second, you're creating a variable, but not giving it a value. You have to create a new array and assign it to the variable. To fix that, add this line after the first one:

    myStringArray = []
    

    ...or you can just change the first line to this:

    var myStringArray: (String,Int)[] = []
    

    After that, this line works fine and you don't have to worry about overloading operators or other craziness. You're done!

    myStringArray += ("One", 1)
    

    Here's the complete solution. A whopping two lines and one wasn't even changed:

    var myStringArray: (String,Int)[] = []
    myStringArray += ("One", 1)
    
    0 讨论(0)
提交回复
热议问题