Skip to content

Fix (nsError as Error).localizedDescription output #967

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
May 17, 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
6 changes: 5 additions & 1 deletion Foundation/NSError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,11 @@ public extension Error where Self : CustomNSError {
public extension Error {
/// Retrieve the localized description for this error.
var localizedDescription: String {
let defaultUserInfo = _swift_Foundation_getErrorDefaultUserInfo(self) as! [String : Any]
if let nsError = self as? NSError {
return nsError.localizedDescription
}

let defaultUserInfo = _swift_Foundation_getErrorDefaultUserInfo(self) as? [String : Any]
return NSError(domain: _domain, code: _code, userInfo: defaultUserInfo).localizedDescription
}
}
Expand Down
7 changes: 7 additions & 0 deletions TestFoundation/TestNSError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class TestNSError : XCTestCase {
static var allTests: [(String, (TestNSError) -> () throws -> Void)] {
return [
("test_LocalizedError_errorDescription", test_LocalizedError_errorDescription),
("test_NSErrorAsError_localizedDescription", test_NSErrorAsError_localizedDescription),
]
}

Expand All @@ -33,4 +34,10 @@ class TestNSError : XCTestCase {
let error = Error()
XCTAssertEqual(error.localizedDescription, "error description")
}

func test_NSErrorAsError_localizedDescription() {
let nsError = NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey: "Localized!"])
let error = nsError as Error
XCTAssertEqual(error.localizedDescription, "Localized!")
}
}