苹果的Swift例子解释道

时间:2015-03-12 20:11:36

标签: swift

我跟随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的数组,但我不明白它们是如何创建值的。

1 个答案:

答案 0 :(得分:3)

以上代码与:

相同
var board = Array<Int>(count: finalSquare + 1, repeatedValue: 0)

[Int]语法优先于Array<Int>,但它们的意思相同。

数组的init如下所示:

init(count: Int, repeatedValue: T)

所以这只是调用初始化程序,初始化程序创建一个finalSquare+1 0的数组。

相关问题