我正在关注 swift programming guide
到目前为止,我已经了解了所有内容,直到下一行,他们为游戏创建了一个棋盘。
let finalSquare = 25
var board = [Int](count: finalSquare + 1, repeatedValue: 0)
我的理解是:
// Create a constant with the value of 25
let finalSquare = 25
// This part will create a array with int values.
var board = [Int]
我不明白的部分:
(count: finalSquare + 1, repeatedValue: 0)
有人可以解释这段代码的作用吗?我知道结果是一个 Int 为 0 的数组,但我不明白它们是如何创建这些值的。
请您参考如下方法:
上面的代码等同于:
var board = Array<Int>(count: finalSquare + 1, repeatedValue: 0)
[Int]语法优先于 Array<Int> , 但它们的意思相同。
数组有一个 init看起来像这样:
init(count: Int, repeatedValue: T)
所以这只是调用了初始化器,初始化器创建了一个数组,其中包含 finalSquare+1 0秒。
