Skip to content

Commit 43d94d7

Browse files
ikesyolorentey
authored andcommitted
Make NSObject.hashValue non-overridable
(cherry picked from commit 9ee6b02)
1 parent a8a5e78 commit 43d94d7

File tree

5 files changed

+28
-5
lines changed

5 files changed

+28
-5
lines changed

Foundation/AffineTransform.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ open class NSAffineTransform : NSObject, NSCopying, NSSecureCoding {
387387
return other === self || (other.affineTransform == self.affineTransform)
388388
}
389389

390-
open override var hashValue: Int {
390+
open override var hash: Int {
391391
return affineTransform.hashValue
392392
}
393393

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

+24-1
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,33 @@ open class NSObject : NSObjectProtocol, Equatable, Hashable {
323323
return self
324324
}
325325

326-
open var hashValue: Int {
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 {
327339
return hash
328340
}
329341

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)
351+
}
352+
330353
/// Returns a Boolean value indicating whether two values are equal.
331354
///
332355
/// Equality is the inverse of inequality. For any values `a` and `b`,

0 commit comments

Comments
 (0)