I want to achieve the following; being able to remove any given instance from an array. However the following is not valid Swift 3 syntax:
extension Array where Element: class { // error: Expected identifier for type name
mutating func remove(_ object: AnyObject) {
if let index = index(where: { $0 === object }) {
remove(at: index)
}
}
}
protocol MyProtocol: class { }
class MyClass: MyProtocol { }
var myInstance = MyClass()
var myArray: [MyProtocol] = [myInstance]
myArray.remove(myInstance)
How would a generic approach work? I don't want to special-case the generic extension to MyProtocol or Equatable.