How do I create an array of tuples?

前端 未结 4 966

I\'m trying to create an array of tuples in Swift, but having great difficulty:

var fun: (num1: Int, num2: Int)[] = (num1: Int, num2: Int)[]()
4条回答
  •  天命终不由人
    2020-12-10 14:13

    This also works:

    var fun:Array<(Int,Int)> = []
    fun += (1,2)
    fun += (3,4)
    

    Oddly though, append wants just one set of parens:

    fun.append(5,6)
    

    If you want the labels for the tuple parts:

    var fun:Array<(num1: Int, num2: Int)> = []
    fun += (1,2)                  // This still works
    fun.append(3,4)               // This does not work
    fun.append(num1: 3, num2: 4)  // but this does work
    

提交回复
热议问题