我已经声明了我的数组
var tile = [[Int]]()
然后我像初始化它的值一样
for (var index = 0; index < 4; index++)
{
for (var sindex = 0; sindex < 4; sindex++)
{
self.tile[index][sindex] = 0 // error here
println("\(index) \(sindex)")
}
}
运行时报错“数组索引超出范围”
最佳答案
正如评论员@C_X 和@MartinR 所说,您的数组是空的。以下是如何根据需要对其进行初始化...
var tile = [[Int]](count:4, repeatedValue: [Int](count: 4, repeatedValue: 0))
for index in 0 ..< 4 {
for sindex in 0 ..< 4 {
tile[index][sindex] = 0 // no error here now...
print("\(index) \(sindex)")
}
}
...当然,for
循环现在是多余的,如果您只想要零!
关于ios - 错误 : "array index out of range" in multidimensional array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32650268/