gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 07:01:34 25 4
gpt4 key购买 nike

来自 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]

关于arrays - Swift 数组初始值设定项语法类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31890010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com