如何创建元组数组?

时间:2022-06-15 16:00:16

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

我正在尝试用Swift创建一个元组数组,但是遇到了很大的困难:

var fun: (num1: Int, num2: Int)[] = (num1: Int, num2: Int)[]()

The above causes a compiler error.

上面的操作会导致编译错误。

Why's that wrong? The following works correctly:

为什么错了?以下是正确的:

var foo: Int[] = Int[]()

4 个解决方案

#1


19  

It works with a type alias:

它使用类型别名:

typealias mytuple = (num1: Int, num2: Int)

var fun: mytuple[] = mytuple[]()
// Or just: var fun = mytuple[]()
fun.append((1,2))
fun.append((3,4))

println(fun)
// [(1, 2), (3, 4)]

Update: As of Xcode 6 Beta 3, the array syntax has changed:

更新:到Xcode 6 Beta 3,数组语法已经改变:

var fun: [mytuple] = [mytuple]()
// Or just: var fun = [mytuple]()

#2


6  

You can do this, just your assignment is overly complicated:

你可以这么做,只是你的作业太复杂了:

var tupleArray: [(num1: Int, num2: Int)] = [ (21, 23) ]

or to make an empty one:

或者做一个空的:

var tupleArray: [(num1: Int, num2: Int)] = []
tupleArray += (1, 2)
println(tupleArray[0].num1)    // prints 1

#3


2  

This also works:

这同样适用:

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

Oddly though, append wants just one set of parens:

奇怪的是,append只需要一组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

#4


0  

Not sure about earlier versions of Swift, but this works in Swift 3 when you want to provide initial values:

对于早期版本的Swift不太确定,但是当您想要提供初始值时,它可以在Swift 3中运行:

var values: [(num1: Int, num2: Int)] = {
    var values = [(num1: Int, num2: Int)]()
    for i in 0..<10 {
        values.append((num1: 0, num2: 0))
    }
    return values
}()

#1


19  

It works with a type alias:

它使用类型别名:

typealias mytuple = (num1: Int, num2: Int)

var fun: mytuple[] = mytuple[]()
// Or just: var fun = mytuple[]()
fun.append((1,2))
fun.append((3,4))

println(fun)
// [(1, 2), (3, 4)]

Update: As of Xcode 6 Beta 3, the array syntax has changed:

更新:到Xcode 6 Beta 3,数组语法已经改变:

var fun: [mytuple] = [mytuple]()
// Or just: var fun = [mytuple]()

#2


6  

You can do this, just your assignment is overly complicated:

你可以这么做,只是你的作业太复杂了:

var tupleArray: [(num1: Int, num2: Int)] = [ (21, 23) ]

or to make an empty one:

或者做一个空的:

var tupleArray: [(num1: Int, num2: Int)] = []
tupleArray += (1, 2)
println(tupleArray[0].num1)    // prints 1

#3


2  

This also works:

这同样适用:

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

Oddly though, append wants just one set of parens:

奇怪的是,append只需要一组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

#4


0  

Not sure about earlier versions of Swift, but this works in Swift 3 when you want to provide initial values:

对于早期版本的Swift不太确定,但是当您想要提供初始值时,它可以在Swift 3中运行:

var values: [(num1: Int, num2: Int)] = {
    var values = [(num1: Int, num2: Int)]()
    for i in 0..<10 {
        values.append((num1: 0, num2: 0))
    }
    return values
}()