3 回答

TA貢獻1816條經(jīng)驗 獲得超4個贊
如Xcode 8 beta 6發(fā)行說明中所述,
Swift定義的錯誤類型可以通過采用新的LocalizedError協(xié)議提供本地化的錯誤描述。
在你的情況下:
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.
如果錯誤轉(zhuǎn)換為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協(xié)議,錯誤可以提供userInfo字典(以及a 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

TA貢獻1802條經(jīng)驗 獲得超4個贊
我還要補充一下,如果你的錯誤有這樣的參數(shù)
enum NetworkError: LocalizedError { case responseStatusError(status: Int, message: String)}
您可以在本地化描述中調(diào)用這些參數(shù),如下所示:
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" }}

TA貢獻1810條經(jīng)驗 獲得超5個贊
使用結(jié)構(gòu)可以是一種替代方案。靜態(tài)本地化有點優(yōu)雅:
import Foundationstruct 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)") }}
- 3 回答
- 0 關(guān)注
- 1022 瀏覽
添加回答
舉報