Skip to content

Commit b0e30dd

Browse files
author
ematejska
authored
Merge pull request #10438 from jrose-apple/4.0-error-code-hash
[Foundation] Make CocoaError.Code and URLError.Code Hashable.
2 parents 3e8e3fb + 7fb21c4 commit b0e30dd

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

stdlib/public/SDK/Foundation/NSError.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,14 +602,18 @@ public struct CocoaError : _BridgedStoredNSError {
602602
public static var _nsErrorDomain: String { return NSCocoaErrorDomain }
603603

604604
/// The error code itself.
605-
public struct Code : RawRepresentable, _ErrorCodeProtocol {
605+
public struct Code : RawRepresentable, Hashable, _ErrorCodeProtocol {
606606
public typealias _ErrorType = CocoaError
607607

608608
public let rawValue: Int
609609

610610
public init(rawValue: Int) {
611611
self.rawValue = rawValue
612612
}
613+
614+
public var hashValue: Int {
615+
return self.rawValue
616+
}
613617
}
614618
}
615619

@@ -1845,14 +1849,18 @@ public struct URLError : _BridgedStoredNSError {
18451849
public static var _nsErrorDomain: String { return NSURLErrorDomain }
18461850

18471851
/// The error code itself.
1848-
public struct Code : RawRepresentable, _ErrorCodeProtocol {
1852+
public struct Code : RawRepresentable, Hashable, _ErrorCodeProtocol {
18491853
public typealias _ErrorType = URLError
18501854

18511855
public let rawValue: Int
18521856

18531857
public init(rawValue: Int) {
18541858
self.rawValue = rawValue
18551859
}
1860+
1861+
public var hashValue: Int {
1862+
return self.rawValue
1863+
}
18561864
}
18571865
}
18581866

test/stdlib/NSError.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,9 @@ tests.test("convenience") {
5353
expectEqual("bar", (error3 as NSError).userInfo["foo"] as? String)
5454
}
5555

56+
tests.test("Hashable") {
57+
checkHashable([CocoaError.Code.fileNoSuchFile, .fileReadUnknown, .keyValueValidation], equalityOracle: { $0 == $1 })
58+
checkHashable([URLError.Code.unknown, .cancelled, .badURL], equalityOracle: { $0 == $1 })
59+
}
60+
5661
runAllTests()

0 commit comments

Comments
 (0)