Xcode 6.3 中不允许 NSObject 的 hash() 和 description()

hash() and description() not allowed in Xcode 6.3 for NSObject

在我的 Swift 图书馆 EVCloudKitDao 我做了很多反思。因此,我将我的数据对象的基础 class 设置为 NSObject。现在升级到 Xcode 6.3 后,我在获取哈希和对象描述的 2 个函数上遇到错误。描述功能很不错,但我确实需要散列来使我的对象与 Set 一起工作。

这是我的代码

public class EVCloudKitDataObject : NSObject, NSCoding, Printable, Hashable, Equatable {

    public func hash() -> Int {
        return self.hashValue
    }
    public func description() -> String {
        return EVReflection.description(self)
    }
}

我得到的错误是:

/Users/evermeer/Desktop/dev/GitHub/EVCloudKitDao/AppMessage/AppMessage/CloudKit/EVCloudKitDataObject.swift:106:17: Method 'hash()' with Objective-C selector 'hash' conflicts with getter for 'hash' from superclass 'NSObject' with the same Objective-C selector

/Users/evermeer/Desktop/dev/GitHub/EVCloudKitDao/AppMessage/AppMessage/CloudKit/EVCloudKitDataObject.swift:86:17: Method 'description()' with Objective-C selector 'description' conflicts with getter for 'description' from superclass 'NSObject' with the same Objective-C selector

有谁知道我该如何解决这个问题? 您不能使用覆盖。

如错误所述,在这两种情况下,属性 和方法之间都存在命名冲突。最明显的修复方法是将您的 2 个方法转换为属性:

public override var hash: Int {
    return self.hashValue
}

public override var description: String {
    return EVReflection.description(self)
}

也可以写成:

public override var hash:Int {
    get {
        return self.hashValue
    }
}
public override var description : String {
    get {
        return EVReflection.description(self)
    }
}

之所以在之前的版本中有效,很有可能是因为:

Swift now detects discrepancies between overloading and overriding in the Swift type system and the effective behavior seen via the Objective-C runtime.

release notes 中阅读更多内容(搜索 1839104618383574