我有这个数组:
var array = [[12,46,["a","b","c"],"nil",true,7,true],[10,40,["c","d","e"],"nil",true,7,true],[0,1,["z","l","w"],"hey",false,7,true]].
当我尝试通过以下代码更改数组boolean的最后一个布尔值:
array[0][6] = false
我收到此错误"无法通过下标下标分配是get-only数组"
答案 0 :(得分:4)
有趣。我认为编译器在某个地方绊倒了。您只需检查数组的类型即可理解原因:
var array = [
[12,46,["a","b","c"],"nil",true,7,true],
[10,40,["c","d","e"],"nil",true,7,true],
[0,1,["z","l","w"],"hey",false,7,true]
]
print(array.dynamicType) // Array<NSArray>
// can't assign to the inner array since NSArray is immutable
给Swift一个内部类型的提示,它可以正常工作
var array: [[AnyObject]] = [
[12,46,["a","b","c"],"nil",true,7,true],
[10,40,["c","d","e"],"nil",true,7,true],
[0,1,["z","l","w"],"hey",false,7,true]
]
array[0][6] = false // works
答案 1 :(得分:2)
您应该使用元组数组。使代码更好,性能更好,不依赖于Objective-C:
var array = [
(12,46,["a","b","c"],"nil",true,7,true),
(10,40,["c","d","e"],"nil",true,7,true),
(0,1,["z","l","w"],"hey",false,7,true)
]
// Array has type [(Int, Int, [String], String, Bool, Int, Bool)]
array[0].6 = false