如何在 Swift 中提供错误类型的本地化描述?
How to provide a localized description with an Error type in Swift?
我正在使用 Swift 3 语法定义自定义错误类型,我想提供一个用户友好的错误描述,该错误由 return localizedDescription
[= Error
对象的 27=]。我该怎么做?
public enum MyError: Error {
case customError
var localizedDescription: String {
switch self {
case .customError:
return NSLocalizedString("A user-friendly description of the error.", comment: "My error")
}
}
}
let error: Error = MyError.customError
error.localizedDescription
// "The operation couldn’t be completed. (MyError error 0.)"
有没有办法让 localizedDescription
变成 return 我的自定义错误描述 ("A user-friendly description of the error.")?请注意,这里的错误对象是 Error
类型而不是 MyError
类型。当然,我可以将对象转换为 MyError
(error as? MyError)?.localizedDescription
但是有没有办法让它在不转换为我的错误类型的情况下工作?
如 Xcode 8 beta 6 发行说明中所述,
Swift-defined error types can provide localized error descriptions by adopting the new LocalizedError protocol.
你的情况:
public enum MyError: Error {
case customError
}
extension MyError: LocalizedError {
public var errorDescription: String? {
switch self {
case .customError:
return NSLocalizedString("A user-friendly description of the error.", comment: "My error")
}
}
}
let error: Error = MyError.customError
print(error.localizedDescription) // A user-friendly description of the error.
如果转换错误,您可以提供更多信息
到 NSError
(这总是可能的):
extension MyError : LocalizedError {
public var errorDescription: String? {
switch self {
case .customError:
return NSLocalizedString("I failed.", comment: "")
}
}
public var failureReason: String? {
switch self {
case .customError:
return NSLocalizedString("I don't know why.", comment: "")
}
}
public var recoverySuggestion: String? {
switch self {
case .customError:
return NSLocalizedString("Switch it off and on again.", comment: "")
}
}
}
let error = MyError.customError as NSError
print(error.localizedDescription) // I failed.
print(error.localizedFailureReason) // Optional("I don\'t know why.")
print(error.localizedRecoverySuggestion) // Optional("Switch it off and on again.")
采用CustomNSError
协议错误可以提供
userInfo
字典(还有 domain
和 code
)。示例:
extension MyError: CustomNSError {
public static var errorDomain: String {
return "myDomain"
}
public var errorCode: Int {
switch self {
case .customError:
return 999
}
}
public var errorUserInfo: [String : Any] {
switch self {
case .customError:
return [ "line": 13]
}
}
}
let error = MyError.customError as NSError
if let line = error.userInfo["line"] as? Int {
print("Error in line", line) // Error in line 13
}
print(error.code) // 999
print(error.domain) // myDomain
如果你的错误有这样的参数,我还要补充
enum NetworkError: LocalizedError {
case responseStatusError(status: Int, message: String)
}
您可以像这样在本地化描述中调用这些参数:
extension NetworkError {
public var errorDescription: String? {
switch self {
case .responseStatusError(status: let status, message: let message):
return "Error with status \(status) and message \(message) was thrown"
}
}
你甚至可以像这样缩短它:
extension NetworkError {
public var errorDescription: String? {
switch self {
case let .responseStatusError(status, message):
return "Error with status \(status) and message \(message) was thrown"
}
}
现在有两种错误采用协议,您的错误类型可以采用它们来向 Objective-C 提供额外信息——LocalizedError 和 CustomNSError。这是一个同时采用它们的错误示例:
enum MyBetterError : CustomNSError, LocalizedError {
case oops
// domain
static var errorDomain : String { return "MyDomain" }
// code
var errorCode : Int { return -666 }
// userInfo
var errorUserInfo: [String : Any] { return ["Hey":"Ho"] };
// localizedDescription
var errorDescription: String? { return "This sucks" }
// localizedFailureReason
var failureReason: String? { return "Because it sucks" }
// localizedRecoverySuggestion
var recoverySuggestion: String? { return "Give up" }
}
这里有更优雅的解决方案:
enum ApiError: String, LocalizedError {
case invalidCredentials = "Invalid credentials"
case noConnection = "No connection"
var localizedDescription: String { return NSLocalizedString(self.rawValue, comment: "") }
}
可以使用结构作为替代方法。静态本地化有点优雅:
import Foundation
struct MyError: LocalizedError, Equatable {
private var description: String!
init(description: String) {
self.description = description
}
var errorDescription: String? {
return description
}
public static func ==(lhs: MyError, rhs: MyError) -> Bool {
return lhs.description == rhs.description
}
}
extension MyError {
static let noConnection = MyError(description: NSLocalizedString("No internet connection",comment: ""))
static let requestFailed = MyError(description: NSLocalizedString("Request failed",comment: ""))
}
func throwNoConnectionError() throws {
throw MyError.noConnection
}
do {
try throwNoConnectionError()
}
catch let myError as MyError {
switch myError {
case .noConnection:
print("noConnection: \(myError.localizedDescription)")
case .requestFailed:
print("requestFailed: \(myError.localizedDescription)")
default:
print("default: \(myError.localizedDescription)")
}
}
这个对我有用:
NSError(domain: "com.your", code: 0, userInfo: [NSLocalizedDescriptionKey: "Error description"])
我正在使用 Swift 3 语法定义自定义错误类型,我想提供一个用户友好的错误描述,该错误由 return localizedDescription
[= Error
对象的 27=]。我该怎么做?
public enum MyError: Error {
case customError
var localizedDescription: String {
switch self {
case .customError:
return NSLocalizedString("A user-friendly description of the error.", comment: "My error")
}
}
}
let error: Error = MyError.customError
error.localizedDescription
// "The operation couldn’t be completed. (MyError error 0.)"
有没有办法让 localizedDescription
变成 return 我的自定义错误描述 ("A user-friendly description of the error.")?请注意,这里的错误对象是 Error
类型而不是 MyError
类型。当然,我可以将对象转换为 MyError
(error as? MyError)?.localizedDescription
但是有没有办法让它在不转换为我的错误类型的情况下工作?
如 Xcode 8 beta 6 发行说明中所述,
Swift-defined error types can provide localized error descriptions by adopting the new LocalizedError protocol.
你的情况:
public enum MyError: Error {
case customError
}
extension MyError: LocalizedError {
public var errorDescription: String? {
switch self {
case .customError:
return NSLocalizedString("A user-friendly description of the error.", comment: "My error")
}
}
}
let error: Error = MyError.customError
print(error.localizedDescription) // A user-friendly description of the error.
如果转换错误,您可以提供更多信息
到 NSError
(这总是可能的):
extension MyError : LocalizedError {
public var errorDescription: String? {
switch self {
case .customError:
return NSLocalizedString("I failed.", comment: "")
}
}
public var failureReason: String? {
switch self {
case .customError:
return NSLocalizedString("I don't know why.", comment: "")
}
}
public var recoverySuggestion: String? {
switch self {
case .customError:
return NSLocalizedString("Switch it off and on again.", comment: "")
}
}
}
let error = MyError.customError as NSError
print(error.localizedDescription) // I failed.
print(error.localizedFailureReason) // Optional("I don\'t know why.")
print(error.localizedRecoverySuggestion) // Optional("Switch it off and on again.")
采用CustomNSError
协议错误可以提供
userInfo
字典(还有 domain
和 code
)。示例:
extension MyError: CustomNSError {
public static var errorDomain: String {
return "myDomain"
}
public var errorCode: Int {
switch self {
case .customError:
return 999
}
}
public var errorUserInfo: [String : Any] {
switch self {
case .customError:
return [ "line": 13]
}
}
}
let error = MyError.customError as NSError
if let line = error.userInfo["line"] as? Int {
print("Error in line", line) // Error in line 13
}
print(error.code) // 999
print(error.domain) // myDomain
如果你的错误有这样的参数,我还要补充
enum NetworkError: LocalizedError {
case responseStatusError(status: Int, message: String)
}
您可以像这样在本地化描述中调用这些参数:
extension NetworkError {
public var errorDescription: String? {
switch self {
case .responseStatusError(status: let status, message: let message):
return "Error with status \(status) and message \(message) was thrown"
}
}
你甚至可以像这样缩短它:
extension NetworkError {
public var errorDescription: String? {
switch self {
case let .responseStatusError(status, message):
return "Error with status \(status) and message \(message) was thrown"
}
}
现在有两种错误采用协议,您的错误类型可以采用它们来向 Objective-C 提供额外信息——LocalizedError 和 CustomNSError。这是一个同时采用它们的错误示例:
enum MyBetterError : CustomNSError, LocalizedError {
case oops
// domain
static var errorDomain : String { return "MyDomain" }
// code
var errorCode : Int { return -666 }
// userInfo
var errorUserInfo: [String : Any] { return ["Hey":"Ho"] };
// localizedDescription
var errorDescription: String? { return "This sucks" }
// localizedFailureReason
var failureReason: String? { return "Because it sucks" }
// localizedRecoverySuggestion
var recoverySuggestion: String? { return "Give up" }
}
这里有更优雅的解决方案:
enum ApiError: String, LocalizedError {
case invalidCredentials = "Invalid credentials"
case noConnection = "No connection"
var localizedDescription: String { return NSLocalizedString(self.rawValue, comment: "") }
}
可以使用结构作为替代方法。静态本地化有点优雅:
import Foundation
struct MyError: LocalizedError, Equatable {
private var description: String!
init(description: String) {
self.description = description
}
var errorDescription: String? {
return description
}
public static func ==(lhs: MyError, rhs: MyError) -> Bool {
return lhs.description == rhs.description
}
}
extension MyError {
static let noConnection = MyError(description: NSLocalizedString("No internet connection",comment: ""))
static let requestFailed = MyError(description: NSLocalizedString("Request failed",comment: ""))
}
func throwNoConnectionError() throws {
throw MyError.noConnection
}
do {
try throwNoConnectionError()
}
catch let myError as MyError {
switch myError {
case .noConnection:
print("noConnection: \(myError.localizedDescription)")
case .requestFailed:
print("requestFailed: \(myError.localizedDescription)")
default:
print("default: \(myError.localizedDescription)")
}
}
这个对我有用:
NSError(domain: "com.your", code: 0, userInfo: [NSLocalizedDescriptionKey: "Error description"])