如何在Swift中提供错误类型的本地化描述? [英] How to provide a localized description with an Error type in Swift?

查看:122
本文介绍了如何在Swift中提供错误类型的本地化描述?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  public enum MyError:Error {
case customError

var localizedDescription:String {
switch self {
case .customError:
return NSLocalizedString(用户友好的错误描述,评论:我的错误)
}
}
}

let error:Error = MyError.customError
error.localizedDescription
//操作无法完成(MyError error 0.)

有一种方法可以将 localizedDescription 返回我的自定义错误描述(用户友好的错误描述)?请注意,此处的错误对象的类型为错误而不是 MyError 。当然,我可以将对象转换为MyError

 (错误为?MyError)?localDescription 

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

解决方案

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


Swift定义错误类型可以通过采用新的LocalizedError协议来提供本地化的错误描述。


在您的情况下:


$



var errorDescription:String? {
switch self {
case .customError:
return NSLocalizedString(用户友好的错误描述,评论:我的错误)
}
}
}

let error:Error = MyError.customError
print(error.localizedDescription)//用户友好的错误描述。

如果错误被转换为
,您可以提供更多信息到 NSError (总是可能的):

 扩展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(我不知道为什么,评论:)
}
}
public var recoverySuggestion:String? {
switch self {
case .customError:
return NSLocalizedString(再次关闭它,评论:)
}
}
}

let error = MyError.customError as NSError
print(error.localizedDescription)//我失败了。
print(error.localizedFailureReason)//可选(我不知道为什么)
print(error.localizedRecoverySuggestion)//可选(重新关闭它)

通过采用 CustomNSError 协议,错误可以提供
a userInfo 字典(以及代码)。示例:

 扩展名MyError:CustomNSError {

public static var errorDomain:String {
返回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

如果let line = error.userInfo [line]如? Int {
print(Error in line,line)//第13行中的错误
}

print(error.code)// 999
print( error.domain)// myDomain


I am defining a custom error type with Swift 3 syntax and I want to provide a user-friendly description of the error which is returned by the localizedDescription property of the Error object. How can I do it?

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

Is there a way for the localizedDescription to return my custom error description ("A user-friendly description of the error.")? Note that the error object here is of type Error and not MyError. I can, of course, cast the object to MyError

(error as? MyError)?.localizedDescription

but is there a way to make it work without casting to my error type?

解决方案

As described in the Xcode 8 beta 6 release notes,

Swift-defined error types can provide localized error descriptions by adopting the new LocalizedError protocol.

In your case:

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.

You can provide even more information if the error is converted to NSError (which is always possible):

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

By adopting the CustomNSError protocol the error can provide a userInfo dictionary (and also a domain and code). Example:

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

这篇关于如何在Swift中提供错误类型的本地化描述?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆