Eha*_*fan 33 nserror ios swift
我正在尝试创建一个错误对象以显示给用户.
let userInfo: [NSObject : AnyObject] =
[
"NSLocalizedDescriptionKey" : NSLocalizedString("Unauthorized", comment: ""),
"NSLocalizedFailureReasonErrorKey" : NSLocalizedString("Unauthorized", comment: "")
]
let err = NSError(domain: "ShiploopHttpResponseErrorDomain", code: httpResponse.statusCode, userInfo: userInfo)
print("Error in Post: \(err.localizedDescription)")
Run Code Online (Sandbox Code Playgroud)
不幸的是输出是:
Error in Post: The operation couldn’t be completed.(ShiploopHttpResponseErrorDomain error 401.)
Run Code Online (Sandbox Code Playgroud)
我希望能够向用户显示他应该激活他的帐户.有任何想法吗??
Pro*_*ier 31
看起来像你想要的(见字典键):
斯威夫特2
let userInfo: [NSObject : AnyObject] =
[
NSLocalizedDescriptionKey : NSLocalizedString("Unauthorized", value: "Please activate your account", comment: ""),
NSLocalizedFailureReasonErrorKey : NSLocalizedString("Unauthorized", value: "Account not activated", comment: "")
]
Run Code Online (Sandbox Code Playgroud)
斯威夫特3
let userInfo: [AnyHashable : Any] =
[
NSLocalizedDescriptionKey : NSLocalizedString("Unauthorized", value: "Please activate your account", comment: "") ,
NSLocalizedFailureReasonErrorKey : NSLocalizedString("Unauthorized", value: "Account not activated", comment: "")
]
Run Code Online (Sandbox Code Playgroud)
然后在swift 2或3中创建错误对象,如下所示:
let err = NSError(domain: "ShiploopHttpResponseErrorDomain", code: 401, userInfo: userInfo)
println("Error in Post: \(err.localizedDescription)")
Run Code Online (Sandbox Code Playgroud)
NSLocalizedDescriptionKey和NSLocalizedFailureReasonErrorKey是全局String变量,以及userInfo字典中的键.值与您指定的值略有不同:
println(NSLocalizedDescriptionKey) //prints "NSLocalizedDescription"
println(NSLocalizedFailureReasonErrorKey) //prints "NSLocalizedFailureReason"
Run Code Online (Sandbox Code Playgroud)
通过右键单击类(本例中为NSError)并在xcode中选择"Jump To Definition"来查看文档是一种很好的做法.各种问题都可以这样回答.:)
Jos*_*nor 25
在Swift 3中创建一个非常简单的错误:
let error = NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey : "Object does not exist"])
Run Code Online (Sandbox Code Playgroud)
Aam*_*irR 10
这是我的Helper方法,它使用一些额外的堆栈跟踪信息创建NSError对象:
let error = NSError(domain: "com.example.error", code: 0, userInfo: [NSLocalizedDescriptionKey: "message"])
Run Code Online (Sandbox Code Playgroud)
用法:
func error(_ message: String, code: Int = 0, domain: String = "com.example.error", function: String = #function, file: String = #file, line: Int = #line) -> NSError {
let functionKey = "\(domain).function"
let fileKey = "\(domain).file"
let lineKey = "\(domain).line"
let error = NSError(domain: domain, code: code, userInfo: [
NSLocalizedDescriptionKey: message,
functionKey: function,
fileKey: file,
lineKey: line
])
// Crashlytics.sharedInstance().recordError(error)
return error
}
Run Code Online (Sandbox Code Playgroud)
请注意 Helper类中的3个注释行,我用它来记录Crashlytics中的错误,您可以取消注释上面的行并使用以下扩展来记录Crashlytics中的错误:
let localizedErrorMessage: String = NSLocalizedString("Unauthorized", comment: "Account not activated")
let error = error(localizedErrorMessage)
Run Code Online (Sandbox Code Playgroud)