Swift:在didFailWithError中处理NSError的Corelocation [英] Swift: Corelocation handling NSError in didFailWithError

查看:273
本文介绍了Swift:在didFailWithError中处理NSError的Corelocation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CoreLocation来成功确定用户的位置。但是当我尝试使用CLLocationManagerDelegate方法时:

I'm using CoreLocation to successfully determine the user's location. However when i try to use the CLLocationManagerDelegate method:

func locationManager(_ manager: CLLocationManager!, didFailWithError error: NSError!)

我遇到了错误术语的问题。

I run into problems with the error term.

func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
    println("didFailWithError \(error)")

    if let err = error {
        if err.code == kCLErrorLocationUnknown {
            return
        }
    }
}

这会导致使用未解析的标识符kCLErrorLocationUnknown错误消息。我知道kCLErrors是枚举,并且它们已经在Swift中进化但我被卡住了。

This results in a 'Use of unresolved identifier kCLErrorLocationUnknown' error message. I know that the kCLErrors are enums and that they have evolved in Swift but I'm stuck.

推荐答案

更新Swift 4:错误现在传递给回调为错误:错误可以强制转换为 CLError

Update for Swift 4: The error is now passed to the callback as error: Error which can be cast to an 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)
    }
}






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


Older answer: The Core Location error codes are defined as

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.code 进行比较, toRaw()
可以使用:

and to compare the enumeration value with the integer err.code, toRaw() can be used:

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

或者,您可以从错误代码创建 CLError 并检查
是否有可能的值:

Alternatively, you can create a CLError from the error code and check that for the possible values:

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

更新: 在Xcode 6.1 beta 2中, fromRaw() toRaw()方法一直是
分别由 init?(rawValue:)初始化程序和 rawValue 属性替换:

UPDATE: In Xcode 6.1 beta 2, the fromRaw() and toRaw() methods have been replaced by an init?(rawValue:) initializer and a rawValue property, respectively:

if err.code == CLError.LocationUnknown.rawValue { ... }

if let clErr = CLError(rawValue: code) { ... }

这篇关于Swift:在didFailWithError中处理NSError的Corelocation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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