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


202

我正在使用Swift 3语法定义自定义错误类型,并且我想提供localizedDescriptionError对象的属性返回的错误的用户友好描述。我该怎么做?

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可以返回我的自定义错误描述(“错误的用户友好描述”。)?请注意,这里的错误对象是类型Error而不是MyError。我当然可以将对象转换为MyError

(error as? MyError)?.localizedDescription

但是有没有办法在不强制转换为我的错误类型的情况下使它工作?

Answers:


401

如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.

如果将错误转换为错误信息,则可以提供更多信息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字典(以及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

7
有没有你为什么做的理由MyErrorError第一和带扩展它 LocalizedError以后呢?如果您LocalizedError首先将其设置为有区别吗?
Gee.E

9
@ Gee.E:没关系。这只是组织代码的一种方法(每个协议一个扩展名)。比较stackoverflow.com/questions/36263892/...stackoverflow.com/questions/40502086/...,或natashatherobot.com/using-swift-extensions
Martin R

4
啊,检查。我明白你现在在说什么。natashatherobot.com/using-swift-extensions上的“协议符合性”部分确实是您所要表达的一个很好的例子。谢谢!
Gee.E

1
@MartinR如果我的错误将被转换为NSError,如何传递可以作为NSError的userInfo访问的错误的字典?
BangOperator

18
当心键入var errorDescription: String?而不是String。LocalizedError的实现中存在错误。参见SR-5858
ethanhuang13 '18

35

如果您的错误具有这样的参数,我也要添加

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"
  }
}

4

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

}

2
您可以编辑吗?您的示例对理解每个示例的价值无济于事。或者只是将其删除,因为MartinR的答案恰好提供了这一点……
Honey

3

使用结构可以是一种替代方法。静态本地化有点优雅:

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)")
   }
}

0

这是更优雅的解决方案:

  enum ApiError: String, LocalizedError {

    case invalidCredentials = "Invalid credentials"
    case noConnection = "No connection"

    var localizedDescription: String { return NSLocalizedString(self.rawValue, comment: "") }

  }

4
这在运行时可能会更好一些,但是静态本地化步骤将无法为翻译程序提取这些字符串。"Bad entry in file – Argument is not a literal string"运行exportLocalizationsgenstrings创建可翻译文本列表时,您会看到一个错误。
savinola

@savinola同意,在这种情况下静态本地化将不起作用。也许使用switch + case只是唯一的选择……
Vitaliy Gozhenko

使用原始值也将防止利用关联的值您的任何错误
布罗迪·罗伯逊
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.