You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In swift-corelibs-foundation, NSObject.hashValue is left overridable, and is in fact overridden by most (all?) Foundation-defined subclasses.
This causes the following compiler warning:
Foundation/NSError.swift:165:23: warning: override of 'NSObject.hashValue' is deprecated; override 'NSObject.hash' to get consistent hashing behavior
override open var hashValue: Int {
^
Subclasses of NSObject are expected to customize hashing by overriding the hash property, not hashValue.
(The Swift-only hashValue property was accidentally left open in the Cocoa Foundation overlays in Swift 4.2 and below, which was causing a great deal of confusion – overriding NSObject.hashValue instead of NSObject.hash made the customization invisible to Cocoa collections, breaking hashing. In Swift 5, NSObject.hashValue is non-overridable on Apple platforms. swift-corelibs-foundation should be updated to follow suit, and to make its hashing APIs consistent with Darwin's.)
The text was updated successfully, but these errors were encountered:
Swift's `Equatable` (and consequently, `Hashable`) protocols aren't directly applicable to class hierarchies like `NSObject`. In particular, Equatable provides no way for subclasses to customize equality – the root class needs to provide some customization hook for this.
`NSObject` does this by exposing the `isEqual(_🙂` method and the `hash` property, respectively. To change how an `NSObject` subclass compares and hashes, override these two methods.
The compiler warning is about an unfortunate footgun – `NSObject.hashValue` was accidentally left overridable in Swift 4.1 and below, even though overriding it has no effect as far as NSObject is concerned. We're planning to turn it into an error in Swift 5.
See the forum discussion below for more details, including sample code for custom NSObject subclasses.
I updated the description to reflect the underlying issue – I'm hijacking this bug to track the swift-corelibs-foundation side of the NSObject.hashValue vs. NSObject.hash cleanup. (rdar://problem/45674813)
Environment
Ubuntu 14.04
Swift 4.2-RELEASE
Additional Detail from JIRA
md5: 89402437120ed0c994dfd9e9e03e8ea8
Issue Description:
In swift-corelibs-foundation, NSObject.hashValue is left overridable, and is in fact overridden by most (all?) Foundation-defined subclasses.
This causes the following compiler warning:
Subclasses of NSObject are expected to customize hashing by overriding the hash property, not hashValue.
(The Swift-only hashValue property was accidentally left open in the Cocoa Foundation overlays in Swift 4.2 and below, which was causing a great deal of confusion – overriding NSObject.hashValue instead of NSObject.hash made the customization invisible to Cocoa collections, breaking hashing. In Swift 5, NSObject.hashValue is non-overridable on Apple platforms. swift-corelibs-foundation should be updated to follow suit, and to make its hashing APIs consistent with Darwin's.)
The text was updated successfully, but these errors were encountered: