Swift 数组初始值设定项语法类型不匹配

Swift array initializer syntax type mismatch

来自Swift编程指南:

You can create an empty array of a certain type using initializer syntax:

var someInts = [Int]()
println("someInts is of type [Int] with \(someInts.count) items.")
// prints "someInts is of type [Int] with 0 items."

Note that the type of the someInts variable is inferred to be [Int] from the type of the initializer.

但是如果你真的把它原样复制到XCode,并检查someInts的类型,你会得到[(Int)],这是一个单元素数组Int 元组。为什么会有这种差异?您还可以按如下方式初始化数组:

var someInts: [Int] = []

类型正确。

实际上这些类型的行为似乎相同,但我想弄清楚这里发生了什么。

还有in the reference doc for Array它说:

Creating an array using this initializer:

var emptyArray = Array<Int>() 

is equivalent to using the convenience syntax:

var equivalentEmptyArray = [Int]()

但是上面的 emptyArray 的类型是 Array<Int>,而 equivalentEmptyArray 的类型是 [(Int)],就像前面的例子一样。 Array<Int> 只是 [Int] 的完整形式,所以这不是真正的问题,只是后者与文档所说的类型不匹配。

注意:通过按住选项并单击变量或选择它并在右侧菜单中的“快速帮助检查器”中查找来查找类型。

你是对的,[Int][(Int)]实际上表现完全一样(事实上它们是一样的)。

不幸的是,我不知道发生这种情况的确切原因,但从 Xcode 7 beta 5(可能还有更早的版本)开始,它只显示类型 [Int].