为了改变嵌套数组,您必须要求
Element : MutableCollection
而不是
Element : Collection
.
还可以定义两个扩展名:只读的只读下标
集合,以及可变集合的读写下标:
extension Collection where Index == Int, Element : Collection, Element.Index == Int {
subscript(indexPath: IndexPath) -> Element.Iterator.Element {
return self[indexPath[0]][indexPath[1]]
}
}
extension MutableCollection where Index == Int, Element : MutableCollection, Element.Index == Int {
subscript(indexPath: IndexPath) -> Element.Iterator.Element {
get {
return self[indexPath[0]][indexPath[1]]
}
set {
self[indexPath[0]][indexPath[1]] = newValue
}
}
}