Skip to content

[Foundation] Make CocoaError.Code and URLError.Code Hashable. #10437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions stdlib/public/SDK/Foundation/NSError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -602,14 +602,18 @@ public struct CocoaError : _BridgedStoredNSError {
public static var _nsErrorDomain: String { return NSCocoaErrorDomain }

/// The error code itself.
public struct Code : RawRepresentable, _ErrorCodeProtocol {
public struct Code : RawRepresentable, Hashable, _ErrorCodeProtocol {
public typealias _ErrorType = CocoaError

public let rawValue: Int

public init(rawValue: Int) {
self.rawValue = rawValue
}

public var hashValue: Int {
return self.rawValue
}
}
}

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

/// The error code itself.
public struct Code : RawRepresentable, _ErrorCodeProtocol {
public struct Code : RawRepresentable, Hashable, _ErrorCodeProtocol {
public typealias _ErrorType = URLError

public let rawValue: Int

public init(rawValue: Int) {
self.rawValue = rawValue
}

public var hashValue: Int {
return self.rawValue
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions test/stdlib/NSError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,9 @@ tests.test("convenience") {
expectEqual("bar", (error3 as NSError).userInfo["foo"] as? String)
}

tests.test("Hashable") {
checkHashable([CocoaError.Code.fileNoSuchFile, .fileReadUnknown, .keyValueValidation], equalityOracle: { $0 == $1 })
checkHashable([URLError.Code.unknown, .cancelled, .badURL], equalityOracle: { $0 == $1 })
}

runAllTests()