如何创建元组数组? [英] How do I create an array of tuples?
本文介绍了如何创建元组数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在 Swift 中创建一个元组数组,但遇到了很大的困难:
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)[]()
以上导致编译器错误.
为什么不对?以下工作正常:
Why's that wrong? The following works correctly:
var foo: Int[] = Int[]()
推荐答案
它与类型别名一起使用:
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)]
更新:从 Xcode 6 Beta 3 开始,数组语法发生了变化:
Update: As of Xcode 6 Beta 3, the array syntax has changed:
var fun: [mytuple] = [mytuple]()
// Or just: var fun = [mytuple]()
这篇关于如何创建元组数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文