我用Swift 3语法定义了一个自定义错误类型,我想提供一个用户友好的错误描述,该错误由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是否有办法返回我的自定义错误描述(“一个用户友好的错误描述。”)?注意,这里的错误对象的类型是error,而不是MyError。当然,我可以将对象强制转换为MyError

(error as? MyError)?.localizedDescription

但是有没有一种方法可以使它工作而不强制转换为错误类型呢?


当前回答

使用结构体也是一种选择。稍微优雅一点的静态本地化:

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

其他回答

enum NetworkError: LocalizedError { 案例noConnection 公共变量描述:字符串{ ///如果你有多个case,你可以在这里切换self。 返回“No internet connection” } //你需要实现' errorDescription '而不是' localizedDescription '。 public var errorDescription:字符串?{ 返回描述 } }

这个对我很有用:

NSError(domain: "com.your", code: 0, userInfo: [NSLocalizedDescriptionKey: "Error description"])

使用结构体也是一种选择。稍微优雅一点的静态本地化:

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

这里有一个更优雅的解决方案:

  enum ApiError: String, LocalizedError {

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

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

  }

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

}