Sven van den Boogaart
Sven van den Boogaart

Reputation: 12323

Swift example by apple explained

Im following the swift programming guide
So far i've understood everything untill the following line, where they create a board for a game.

let finalSquare = 25
var board = [Int](count: finalSquare + 1, repeatedValue: 0)

for what I understand is:

// Create a constant with the value of 25
let finalSquare = 25

// This part will create a array with int values.
var board = [Int] 

The part I dont understand :

 (count: finalSquare + 1, repeatedValue: 0)

Could someone explain what this code does ? I know the result is an array with Int's 0 but i dont understand how they create the values.

Upvotes: 1

Views: 66

Answers (1)

Rob Napier
Rob Napier

Reputation: 299265

The above code is identical to:

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

The [Int] syntax is preferred over Array<Int>, but they mean the same thing.

Array has an init that looks like this:

init(count: Int, repeatedValue: T)

So this is just calling that initializer, and that initializer creates an array that has finalSquare+1 0s.

Upvotes: 3

Related Questions