我試圖擴展Array<MutatingCollection>
,所以我可以鏡像數組的內容,但編譯器說我不能調用reverse()
上的元素數組,儘管在MutatingCollection
協議中定義了reverse()
。Array <MutableCollection>的Swift擴展將不允許reverse()
我想要做這樣的事情:
var table = [[0,1,2],
[3,4,5],
[6,7,8]]
table.mirror()
//table now [[2,1,0],
// [5,4,3],
// [8,7,6]]
這裏是我的(不工作)代碼:
extension Array where Element == MutableCollection {
mutating func mirror() {
for index in self.indices {
self[index].reverse()
}
}
}
我已經嘗試過作爲self.map {array in array.reverse()}
以及(我認爲做同樣的事情,但我不完全grok map()
)兩種方式導致相同的錯誤消息:
Member 'reverse' cannot be used on value of type 'MutableCollection'
編輯:我可以直接調用相同的代碼,它按照我的意圖工作。
也許我使用extension
不當,或斯威夫特遊樂場以某種方式阻礙了我的訪問。
此崩潰如果'Element'是一個可變的集合,但不是一個數組,例如爲'var table = [[0,1,2] .dropFirst(),[3,4,5],[6,7,8]]',其中'Element'爲'ArraySlice'。 –
@MartinR我相信我已經確定了在這個問題中提出的問題的原因,並且糾正了你提到的問題。請隨時檢查我的正確性。 – ColGraff
通過額外的約束'Indices.Iterator.Element == Index',您可以擺脫人工的'guard let index = index ...',比較https://stackoverflow.com/a/44457590/1187415或https: //stackoverflow.com/a/40331858/1187415。在Swift 4中不應該有必要。 –