如何在 Swift 中为 NSError 分配错误代码?下面给出了一个编译错误,说“无法分配给‘e’中的‘代码’”:
var e = NSError()
e.code = 6;
还有为什么下面的代码会产生'Int is not convertible to selector'的错误
if e.code == EKAuthorizationStatus.Restricted
{
}
最佳答案
错误代码属性是只读的,类型为 Int。你需要初始化如下:
var e= NSError(domain: <string?>, code: 6, userInfo: <AnyObject>)
更新: 正如@ahruss 所指出的,您还需要将枚举转换为其原始形式,因为类型转换不是隐式的,因此我相应地更新了示例。
请查找有关 NSError 类的文档 here .
Playground 示例:
import Cocoa
var e = NSError(domain: nil, code: 6, userInfo: nil);
//if e.code == 6
if e.code == EKAuthorizationStatus.Restricted.toRaw()
{
println("success");
}
关于swift - 关于swift中NSError的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25191877/