Swift:在didFailWithError中处理NSError的Corelocation

时间:2022-12-26 00:18:18

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

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

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
        }
    }
}

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.

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

3 个解决方案

#1


27  

Update for Swift 4: The error is now passed to the callback as error: Error which can be cast to an CLError:

更新Swift 4:错误现在作为错误传递给回调:错误可以转换为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
    // ...
}

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

并且要将枚举值与整数err.code进行比较,可以使用toRaw():

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

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

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

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

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:

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

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

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

#2


7  

In Swift 3 it's now:

在Swift 3中它现在是:

if error._code == CLError.denied.rawValue { ... }

#3


1  

Swift 4.1:

Swift 4.1:

func locationManager(_: CLLocationManager, didFailWithError error: Error) {
    let err = CLError.Code(rawValue: (error as NSError).code)!
    switch err {
    case .locationUnknown:
        break
    default:
        print(err)
    }
}

#1


27  

Update for Swift 4: The error is now passed to the callback as error: Error which can be cast to an CLError:

更新Swift 4:错误现在作为错误传递给回调:错误可以转换为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
    // ...
}

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

并且要将枚举值与整数err.code进行比较,可以使用toRaw():

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

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

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

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

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:

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

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

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

#2


7  

In Swift 3 it's now:

在Swift 3中它现在是:

if error._code == CLError.denied.rawValue { ... }

#3


1  

Swift 4.1:

Swift 4.1:

func locationManager(_: CLLocationManager, didFailWithError error: Error) {
    let err = CLError.Code(rawValue: (error as NSError).code)!
    switch err {
    case .locationUnknown:
        break
    default:
        print(err)
    }
}