如何在Swift中提供具有错误类型的本地化描述?

Evg*_*nii 175 error-handling nslocalizedstring swift swift3

我正在使用Swift 3语法定义自定义错误类型,我想提供一个用户友好的错误描述,该描述由对象的localizedDescription属性返回Error.我该怎么做?

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.)"
Run Code Online (Sandbox Code Playgroud)

有没有办法让localizedDescription我返回我的自定义错误描述("用户友好的错误描述.")?请注意,此处的错误对象是类型Error而不是MyError.当然,我可以将对象强制转换为MyError

(error as? MyError)?.localizedDescription
Run Code Online (Sandbox Code Playgroud)

但是有没有办法让它工作而不会转换为我的错误类型?

Mar*_*n R 346

如Xcode 8 beta 6发行说明中所述,

Swift定义的错误类型可以通过采用新的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.
Run Code Online (Sandbox Code Playgroud)

如果错误转换为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.")
Run Code Online (Sandbox Code Playgroud)

通过采用该CustomNSError协议,错误可以提供userInfo字典(以及a domaincode).例:

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
Run Code Online (Sandbox Code Playgroud)

  • 注意输入`var errorDescription:String?`而不是**`String`**.LocalizedError的实现存在一个错误.见[SR-5858](https://bugs.swift.org/browse/SR-5858). (14认同)
  • @ Gee.E:没有区别.它只是一种组织代码的方法(每个协议的一个扩展).比较http://stackoverflow.com/questions/36263892/extensions-in-my-own-custom-class,http://stackoverflow.com/questions/40502086/how-to-properly-use-class-extensions-in -swift,或https://www.natashatherobot.com/using-swift-extensions/. (8认同)
  • 是否有理由首先将`MyError`作为`Error`并使用`LocalizedError`扩展它?如果你首先把它变成了"LocalizedError",那会有区别吗? (6认同)
  • 啊,检查一下.我得到你现在说的话.https://www.natashatherobot.com/using-swift-extensions/上的"协议一致性"部分确实是您的意思的一个很好的例子.谢谢! (3认同)

Rez*_*ian 29

我还要补充一下,如果你的错误有这样的参数

enum NetworkError: LocalizedError {
  case responseStatusError(status: Int, message: String)
}
Run Code Online (Sandbox Code Playgroud)

您可以在本地化描述中调用这些参数,如下所示:

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"
  }
}
Run Code Online (Sandbox Code Playgroud)

你甚至可以这样缩短:

extension NetworkError {
  public var errorDescription: String? {
    switch self {
    case let .responseStatusError(status, message):
      return "Error with status \(status) and message \(message) was thrown"
  }
}
Run Code Online (Sandbox Code Playgroud)


Ric*_*yen 11

这对我有用:

NSError(domain: "com.your", code: 0, userInfo: [NSLocalizedDescriptionKey: "Error description"])
Run Code Online (Sandbox Code Playgroud)


mat*_*att 6

现在,您的错误类型可以采用两种错误采用协议,以便为 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" }

}
Run Code Online (Sandbox Code Playgroud)

  • 你能编辑一下吗?您的示例对理解每个示例的价值没有多大帮助。或者只是删除它,因为 MartinR 的答案正是提供了这一点...... (3认同)