I am attempting to extend Array<MutatingCollection>
so I can mirror the contents of an Array of Arrays, but the compiler says I can't call reverse()
on the elements in the array, despite reverse()
being defined in MutatingCollection
protocol.
I want to do something like this:
var table = [[0,1,2],
[3,4,5],
[6,7,8]]
table.mirror()
//table now [[2,1,0],
// [5,4,3],
// [8,7,6]]
Here is my (not working) code:
extension Array where Element == MutableCollection {
mutating func mirror() {
for index in self.indices {
self[index].reverse()
}
}
}
I have tried it as self.map {array in array.reverse()}
as well (which I think does the same thing, but I don't fully grok map()
) Both ways result in the same error message:
Member 'reverse' cannot be used on value of type 'MutableCollection'
Edit: I can call the same code directly and it works as I intended.
Perhaps I'm using extension
improperly, or Swift Playgrounds is blocking my access somehow.