나는 다음과 같은 것을 달성하고 싶다; 주어진 인스턴스를 배열에서 제거 할 수 있습니다. 그러나 다음은 Swift 3 구문이 유효하지 않습니다.스위프트 일반 배열에서 인스턴스를 제거
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)
일반적인 접근 방식은 어떻게 작동합니까? MyProtocol 또는 Equatable에 대한 일반 확장을 특별하게는 원하지 않습니다.
모든'Array' 인스턴스에'remove' 메소드를 추가 할 것이고, Asker는이 메소드를 클래스를 포함하는 배열로만 제한하려고합니다. – Cristik