符合Swift NSObject子类的Objective-C协议的问题
这段代码在Swift 1.1中绝对正常。
This piece of code works absolutely fine in Swift 1.1
// Obj-C
@import Foundation;
@protocol HashableObject <NSObject>
- (NSUInteger)hash;
@end
// Swift
import Foundation
@objc class Object: NSObject, HashableObject {
func hash() -> UInt {
return 0
}
}
然而,在最新的Swift 1.2和XCode 6.3beta2编译器抱怨方法'hash()'覆盖来自超类'NSObject'的Objective-C方法'hash'
However, in latest Swift 1.2 and XCode 6.3beta2 the compiler complains that Method 'hash()' overrides Objective-C method 'hash' from superclass 'NSObject'
这是一个错误,还是其他一些根本改变了,代码错了?这有什么变通方法吗?我的代码中有很多东西需要符合Objective-C库中的某些协议,除了等待下一个Swift版本之外,这基本上没有明显的解决方案。
Is that a bug, or something else has fundamentally changed and the code is wrong? Are there any workarounds to this? I have numerous things in my code that need to conform to certain protocols from Objective-C libraries and this essentially breaks everything with no apparent solution other than wait for the next Swift release.
在Swift 1.2(Xcode 6.3 beta 2)中,你可以覆盖的
作为计算属性:哈希
属性
> NSObject
In Swift 1.2 (Xcode 6.3 beta 2) you can override the hash
property
of NSObject
as a computed property:
class Object: NSObject {
override var hash: Int {
return 0
}
}