Skip to content

Commit 9ee6b02

Browse files
committed
Make NSObject.hashValue non-overridable
1 parent 142c8c8 commit 9ee6b02

File tree

5 files changed

+29
-6
lines changed

5 files changed

+29
-6
lines changed

Foundation/AffineTransform.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ open class NSAffineTransform : NSObject, NSCopying, NSSecureCoding {
393393
return other === self || (other.affineTransform == self.affineTransform)
394394
}
395395

396-
open override var hashValue: Int {
396+
open override var hash: Int {
397397
return affineTransform.hashValue
398398
}
399399

Foundation/NSCache.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fileprivate class NSCacheKey: NSObject {
2929
super.init()
3030
}
3131

32-
override var hashValue: Int {
32+
override var hash: Int {
3333
switch self.value {
3434
case let nsObject as NSObject:
3535
return nsObject.hashValue

Foundation/NSCalendar.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1334,7 +1334,7 @@ internal class _NSCopyOnWriteCalendar: NSCalendar {
13341334
return backingCalendar.isEqual(value)
13351335
}
13361336

1337-
override var hashValue: Int {
1337+
override var hash: Int {
13381338
return backingCalendar.hashValue
13391339
}
13401340

Foundation/NSError.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ open class NSError : NSObject, NSCopying, NSSecureCoding, NSCoding {
162162
// -- NSObject Overrides --
163163
// The compiler has special paths for attempting to do some bridging on NSError (and equivalent Error instances) -- in particular, in the lookup of NSError objects' superclass.
164164
// On platforms where we don't have bridging (i.e. Linux), this causes a silgen failure. We can avoid the issue by overriding methods inherited by NSObject ourselves.
165-
override open var hashValue: Int {
165+
override open var hash: Int {
166166
// CFHash does the appropriate casting/bridging on platforms where we support it.
167167
return Int(bitPattern: CFHash(self))
168168
}

Foundation/NSObject.swift

+25-2
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,31 @@ open class NSObject : NSObjectProtocol, Equatable, Hashable {
323323
return self
324324
}
325325

326-
open func hash(into hasher: inout Hasher) {
327-
hasher.combine(hash)
326+
/// The hash value.
327+
///
328+
/// `NSObject` implements this by returning `self.hash`.
329+
///
330+
/// `NSObject.hashValue` is not overridable; subclasses can customize hashing
331+
/// by overriding the `hash` property.
332+
///
333+
/// **Axiom:** `x == y` implies `x.hashValue == y.hashValue`
334+
///
335+
/// - Note: the hash value is not guaranteed to be stable across
336+
/// different invocations of the same program. Do not persist the
337+
/// hash value across program runs.
338+
public final var hashValue: Int {
339+
return hash
340+
}
341+
342+
/// Hashes the essential components of this value by feeding them into the
343+
/// given hasher.
344+
///
345+
/// NSObject implements this by feeding `self.hash` to the hasher.
346+
///
347+
/// `NSObject.hash(into:)` is not overridable; subclasses can customize
348+
/// hashing by overriding the `hash` property.
349+
public final func hash(into hasher: inout Hasher) {
350+
hasher.combine(self.hash)
328351
}
329352

330353
/// Returns a Boolean value indicating whether two values are equal.

0 commit comments

Comments
 (0)