Swift:Corelocation处理didFailWithError中的NSError

Swift:Corelocation处理didFailWithError中的NSError,第1张

Swift:Corelocation处理didFailWithError中的NSError

Swift 4更新: 错误现在传递给回调

error: Error
,可以将其转换为
CLError

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {    if let clErr = error as? CLError {        switch clErr {        case CLError.locationUnknown: print("location unknown")        case CLError.denied: print("denied")        default: print("other Core Location error")        }    } else {        print("other error:", error.localizedDescription)    }}

较旧的答案: 核心位置错误代码定义为

enum CLError : Int {    case LocationUnknown // location is currently unknown, but CL will keep trying    case Denied // Access to location or ranging has been denied by the user    // ...}

并将枚举值与整数进行比较

err.pre
toRaw()
可以使用:

if err.pre == CLError.LocationUnknown.toRaw() { ...

或者,您可以

CLError
根据错误代码创建一个,然后检查是否有可能的值:

if let clErr = CLError.fromRaw(err.pre) {    switch clErr {    case .LocationUnknown:        println("location unknown")    case .Denied:        println("denied")    default:        println("unknown Core Location error")    }} else {    println("other error")}

更新: 在Xpre 6.1 beta
2中,

fromRaw()
toRaw()
方法分别被
init?(rawValue:)
初始化器和
rawValue
属性代替:

if err.pre == CLError.LocationUnknown.rawValue { ... }if let clErr = CLError(rawValue: pre) { ... }


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5622514.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-15

发表评论

登录后才能评论

评论列表(0条)

保存