如何在Swift中提供一个错误types的本地化描述?
我正在用Swift 3语法定义一个自定义错误types,我想提供Error
对象的localizedDescription
属性返回的错误的用户友好描述。 我该怎么做?
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
返回我自定义的错误描述(“一个用户友好的错误描述”)? 请注意,这里的错误对象的types是Error
而不是MyError
。 当然,我可以将对象投射到MyError
(error as? MyError)?.localizedDescription
但有没有办法使它的工作,而不是铸造我的错误types?
如Xcode 8 beta 6发行说明中所述,
Swift定义的错误types可以通过采用新的LocalizedError协议来提供本地化的错误描述。
在你的情况下:
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: Error { case responseStatusError(status: Int, message: String) }
你可以在你的本地化描述中调用这些参数:
extension NetworkError: LocalizedError { var localizedDescription: String { switch self { case .responseStatusError(status: let status, message: let message): return "Error with status \(status) and message \(message) was thrown" } }
你甚至可以这样缩短:
extension NetworkError: LocalizedError { var localizedDescription: String { switch self { case let .responseStatusError(status, message): return "Error with status \(status) and message \(message) was thrown" } }
现在有两种错误采用协议,您的错误types可以采用以向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" } }
这里是更优雅的解决scheme:
enum ApiError: String, LocalizedError { case invalidCredentials = "Invalid credentials" case noConnection = "No connection" var localizedDescription: String { return NSLocalizedString(self.rawValue, comment: "") } }