amstrudy amstrudy - 5 months ago 14
Swift Question

3x3 array in Swift

I'm trying to make a 3x3 array in Swift, but the number of rows is always different than what I expect. For example, I thought the code below would make a 3x3 array, but it's actually 9x3 array. Why? And how can I make it 3x3?

var NumColumns = 3
var NumRows = 3
var occupied = [[Bool]](count: NumColumns, repeatedValue:[Bool](count: NumRows, repeatedValue:false));
for item in occupied {
for item in occupied {
print(item)
}
}

Answer

Loop typo misuse:

for item in occupied {
  for innerItem in item {
    print(innerItem) // prints 9 times
  }
}
Comments