Cra*_*lot 17 nscoding nsobject swift equatable
FooBar下面的类必须覆盖==该Equatable类型的功能.
但是,调用对象contains数组FooBar不会导致==调用自定义函数内的断点.是否可能其他==功能覆盖此自定义功能?
注意:因为FooBar必须从NSCoding和NSObject继承,所以FooBar不会将Equatable列为协议,因为它会导致此错误:
'FooBar'与协议'Equatable'的冗余一致性
func ==(lhs: FooBar, rhs: FooBar) -> Bool {
return lhs.id == rhs.id
}
class FooBar: NSObject, NSCoding {
// Class def
}
// Both serverFooBars and gFooBars are [FooBar]
let newFooBars = serverFooBars.filter { !gFooBars.contains($0) }
Run Code Online (Sandbox Code Playgroud)
Bla*_*ley 47
因为您的类继承自NSObject您不需要使用swift协议,所以Equatable您必须覆盖该NSObject方法isEquals:
Swift 3.x
class FooBar: NSObject, NSCoding {
override func isEqual(_ object: Any?) -> Bool {
return id == (object as? FooBar)?.id
}
}
Run Code Online (Sandbox Code Playgroud)
(感谢堪察加)
Swift 2.x
class FooBar: NSObject, NSCoding {
override func isEqual(object: AnyObject?) -> Bool {
return id == (object as? FooBar)?.id
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6723 次 |
| 最近记录: |