Skip to content

Commit 4175bef

Browse files
committed
Modernize hashing in Foundation’s RawRepresentable enumerations
1 parent ca19ca8 commit 4175bef

11 files changed

+47
-43
lines changed

Foundation/FileManager.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -828,11 +828,11 @@ public struct FileAttributeKey : RawRepresentable, Equatable, Hashable {
828828
public init(rawValue: String) {
829829
self.rawValue = rawValue
830830
}
831-
832-
public var hashValue: Int {
833-
return self.rawValue.hashValue
831+
832+
public func hash(into hasher: inout Hasher) {
833+
hasher.combine(rawValue)
834834
}
835-
835+
836836
public static func ==(_ lhs: FileAttributeKey, _ rhs: FileAttributeKey) -> Bool {
837837
return lhs.rawValue == rhs.rawValue
838838
}
@@ -873,8 +873,8 @@ public struct FileAttributeType : RawRepresentable, Equatable, Hashable {
873873
self.rawValue = rawValue
874874
}
875875

876-
public var hashValue: Int {
877-
return self.rawValue.hashValue
876+
public func hash(into hasher: inout Hasher) {
877+
hasher.combine(rawValue)
878878
}
879879

880880
public static func ==(_ lhs: FileAttributeType, _ rhs: FileAttributeType) -> Bool {

Foundation/HTTPCookie.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public struct HTTPCookiePropertyKey : RawRepresentable, Equatable, Hashable {
1818
self.rawValue = rawValue
1919
}
2020

21-
public var hashValue: Int {
22-
return self.rawValue.hashValue
21+
public func hash(into hasher: inout Hasher) {
22+
hasher.combine(rawValue)
2323
}
2424

2525
public static func ==(_ lhs: HTTPCookiePropertyKey, _ rhs: HTTPCookiePropertyKey) -> Bool {

Foundation/NSAttributedString.swift

+6-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ extension NSAttributedString {
2121
self.rawValue = rawValue
2222
}
2323

24-
public var hashValue: Int {
25-
return rawValue.hashValue
24+
public func hash(into hasher: inout Hasher) {
25+
hasher.combine(rawValue)
26+
}
27+
28+
public static func ==(left: NSAttributedString.Key, right: NSAttributedString.Key) -> Bool {
29+
return left.rawValue == right.rawValue
2630
}
2731
}
2832
}

Foundation/NSCalendar.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ extension NSCalendar {
6565
public init(rawValue: String) {
6666
self.rawValue = rawValue
6767
}
68-
69-
public var hashValue: Int {
70-
return rawValue.hashValue
68+
69+
public func hash(into hasher: inout Hasher) {
70+
hasher.combine(rawValue)
7171
}
7272

7373
public static let gregorian = NSCalendar.Identifier("gregorian")

Foundation/NSDecimalNumber.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public struct NSExceptionName : RawRepresentable, Equatable, Hashable {
1818
public init(rawValue: String) {
1919
self.rawValue = rawValue
2020
}
21-
22-
public var hashValue: Int {
23-
return self.rawValue.hashValue
21+
22+
public func hash(into hasher: inout Hasher) {
23+
hasher.combine(rawValue)
2424
}
2525

2626
public static func ==(_ lhs: NSExceptionName, _ rhs: NSExceptionName) -> Bool {

Foundation/NSLocale.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ extension NSLocale {
176176
public init(rawValue: String) {
177177
self.rawValue = rawValue
178178
}
179-
180-
public var hashValue: Int {
181-
return rawValue.hashValue
179+
180+
public func hash(into hasher: inout Hasher) {
181+
hasher.combine(rawValue)
182182
}
183183

184184
public static let identifier = NSLocale.Key(rawValue: "kCFLocaleIdentifierKey")

Foundation/NSNotification.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ open class NSNotification: NSObject, NSCopying, NSCoding {
1313
public init(rawValue: String) {
1414
self.rawValue = rawValue
1515
}
16-
17-
public var hashValue: Int {
18-
return self.rawValue.hashValue
16+
17+
public func hash(into hasher: inout Hasher) {
18+
hasher.combine(rawValue)
1919
}
2020

2121
public static func ==(lhs: Name, rhs: Name) -> Bool {

Foundation/NSURL.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ public struct URLResourceKey : RawRepresentable, Equatable, Hashable {
7676
self.rawValue = rawValue
7777
}
7878

79-
public var hashValue: Int {
80-
return rawValue.hashValue
79+
public func hash(into hasher: inout Hasher) {
80+
hasher.combine(rawValue)
8181
}
8282

8383
public static func ==(lhs: URLResourceKey, rhs: URLResourceKey) -> Bool {
@@ -198,8 +198,8 @@ public struct URLFileResourceType : RawRepresentable, Equatable, Hashable {
198198
self.rawValue = rawValue
199199
}
200200

201-
public var hashValue: Int {
202-
return rawValue.hashValue
201+
public func hash(into hasher: inout Hasher) {
202+
hasher.combine(rawValue)
203203
}
204204

205205
public static func ==(lhs: URLFileResourceType, rhs: URLFileResourceType) -> Bool {

Foundation/Progress.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ open class Progress : NSObject {
399399
public let rawValue: String
400400
public init(_ rawValue: String) { self.rawValue = rawValue }
401401
public init(rawValue: String) { self.rawValue = rawValue }
402-
public var hashValue: Int { return self.rawValue.hashValue }
402+
public func hash(into hasher: inout Hasher) { hasher.combine(rawValue) }
403403
public static func ==(_ lhs: FileOperationKind, _ rhs: FileOperationKind) -> Bool { return lhs.rawValue == rhs.rawValue }
404404

405405
/// Use for indicating the progress represents a download.
@@ -478,7 +478,7 @@ public struct ProgressKind : RawRepresentable, Equatable, Hashable {
478478
public let rawValue: String
479479
public init(_ rawValue: String) { self.rawValue = rawValue }
480480
public init(rawValue: String) { self.rawValue = rawValue }
481-
public var hashValue: Int { return self.rawValue.hashValue }
481+
public func hash(into hasher: inout Hasher) { hasher.combine(rawValue) }
482482
public static func ==(_ lhs: ProgressKind, _ rhs: ProgressKind) -> Bool { return lhs.rawValue == rhs.rawValue }
483483

484484
/// Indicates that the progress being performed is related to files.
@@ -491,7 +491,7 @@ public struct ProgressUserInfoKey : RawRepresentable, Equatable, Hashable {
491491
public let rawValue: String
492492
public init(_ rawValue: String) { self.rawValue = rawValue }
493493
public init(rawValue: String) { self.rawValue = rawValue }
494-
public var hashValue: Int { return self.rawValue.hashValue }
494+
public func hash(into hasher: inout Hasher) { hasher.combine(rawValue) }
495495
public static func ==(_ lhs: ProgressUserInfoKey, _ rhs: ProgressUserInfoKey) -> Bool { return lhs.rawValue == rhs.rawValue }
496496

497497
/// How much time is probably left in the operation, as an NSNumber containing a number of seconds.

Foundation/Stream.swift

+12-12
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ extension Stream {
2828
public init(rawValue: String) {
2929
self.rawValue = rawValue
3030
}
31-
32-
public var hashValue: Int {
33-
return rawValue.hashValue
31+
32+
public func hash(into hasher: inout Hasher) {
33+
hasher.combine(rawValue)
3434
}
35-
35+
3636
public static func ==(lhs: Stream.PropertyKey, rhs: Stream.PropertyKey) -> Bool {
3737
return lhs.rawValue == rhs.rawValue
3838
}
@@ -303,8 +303,8 @@ public struct StreamSocketSecurityLevel : RawRepresentable, Equatable, Hashable
303303
public init(rawValue: String) {
304304
self.rawValue = rawValue
305305
}
306-
public var hashValue: Int {
307-
return rawValue.hashValue
306+
public func hash(into hasher: inout Hasher) {
307+
hasher.combine(rawValue)
308308
}
309309
public static func ==(lhs: StreamSocketSecurityLevel, rhs: StreamSocketSecurityLevel) -> Bool {
310310
return lhs.rawValue == rhs.rawValue
@@ -325,8 +325,8 @@ public struct StreamSOCKSProxyConfiguration : RawRepresentable, Equatable, Hasha
325325
public init(rawValue: String) {
326326
self.rawValue = rawValue
327327
}
328-
public var hashValue: Int {
329-
return rawValue.hashValue
328+
public func hash(into hasher: inout Hasher) {
329+
hasher.combine(rawValue)
330330
}
331331
public static func ==(lhs: StreamSOCKSProxyConfiguration, rhs: StreamSOCKSProxyConfiguration) -> Bool {
332332
return lhs.rawValue == rhs.rawValue
@@ -347,8 +347,8 @@ public struct StreamSOCKSProxyVersion : RawRepresentable, Equatable, Hashable {
347347
public init(rawValue: String) {
348348
self.rawValue = rawValue
349349
}
350-
public var hashValue: Int {
351-
return rawValue.hashValue
350+
public func hash(into hasher: inout Hasher) {
351+
hasher.combine(rawValue)
352352
}
353353
public static func ==(lhs: StreamSOCKSProxyVersion, rhs: StreamSOCKSProxyVersion) -> Bool {
354354
return lhs.rawValue == rhs.rawValue
@@ -366,8 +366,8 @@ public struct StreamNetworkServiceTypeValue : RawRepresentable, Equatable, Hasha
366366
public init(rawValue: String) {
367367
self.rawValue = rawValue
368368
}
369-
public var hashValue: Int {
370-
return rawValue.hashValue
369+
public func hash(into hasher: inout Hasher) {
370+
hasher.combine(rawValue)
371371
}
372372
public static func ==(lhs: StreamNetworkServiceTypeValue, rhs: StreamNetworkServiceTypeValue) -> Bool {
373373
return lhs.rawValue == rhs.rawValue

Foundation/StringEncodings.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ extension String {
6868
}
6969

7070
extension String.Encoding : Hashable {
71-
public var hashValue : Int {
72-
return rawValue.hashValue
71+
public func hash(into hasher: inout Hasher) {
72+
hasher.combine(rawValue)
7373
}
7474

7575
public static func ==(lhs: String.Encoding, rhs: String.Encoding) -> Bool {

0 commit comments

Comments
 (0)