I have been struggling to implement an extension for Swift Array to remove an element by value. There are proposed solutions that use down casting, which I am trying to avoid due to the cost associated with dynamic type checking. Would you be able to point out how to get the following code to compile.
Thanks,
protocol Removable: SequenceType {
typealias Element
mutating func remove(v: Element) -> Bool
}
func removeElementByValue<T: Equatable>(inout array: [T], valueToRemove: T) -> Bool {
for (index, value) in enumerate(array) {
if value == valueToRemove {
array.removeAtIndex(index)
return true
}
}
return false
}
extension Array: Removable {
mutating func remove(v: T) -> Bool {
return removeElementByValue(&self, v) // compile error here
}
// tried also with:
// mutating func remove<E:Equatable where E == T>(v: T) -> Bool
}