【发布时间】:2018-11-28 11:01:34
【问题描述】:
我想为数组数组添加一个扩展,以检索大小为 2 的 IndexPath 的 Element:
let array: [[String]] = ....
let indexPath = IndexPath(indexes: [0, 0])
let string = array[indexPath]
在实现以下扩展时,我收到一个错误无法通过下标分配下标是只获取:
extension Array where Element : Collection, Element.Index == Int {
subscript(indexPath: IndexPath) -> Element.Iterator.Element {
get {
return self[indexPath.section][indexPath.item]
}
set {
self[indexPath.section][indexPath.item] = newValue
}
}
}
出现这种错误的原因是什么?如何向下标添加变异选项?
【问题讨论】:
标签: ios arrays swift collections subscript