【问题标题】:CLGeocoder().geocodeAddressString error codesCLGeocoder().geocodeAddressString 错误代码
【发布时间】:2021-01-21 22:59:47
【问题描述】:

我正在开展一个项目,我使用 CLGeocoder 来确定特定位置的地标。 像这样非常简单的调用。

CLGeocoder().geocodeAddressString(location) { (placemarks, error) in
            if let error = error {
                print(error.localizedDescription)
            }

我希望能够捕获 CLError 代码并做出相应的响应,而不是打印出 NSError 的本地化描述。例如,如果找不到位置,我的本地化描述打印

The operation couldn’t be completed. (kCLErrorDomain error 8.)

我怎样才能在 Swift 中确定 CLError 代码,这样我就不必打开一些会根据用户的语言环境而改变的本地化描述? 这可以做到吗? 我只想打开几个案例。

【问题讨论】:

    标签: swift xcode error-handling clgeocoder


    【解决方案1】:

    你需要将错误转换为CLError,然后切换错误code

    if let error = error as? CLError { 
        switch error.code {
        case .locationUnknown:
            print("locationUnknown: location manager was unable to obtain a location value right now.")
        case .denied:
            print("denied: user denied access to the location service.")
        case .promptDeclined:
            print("promptDeclined: user didn’t grant the requested temporary authorization.")
        case .network:
            print("network: network was unavailable or a network error occurred.")
        case .headingFailure:
            print("headingFailure: heading could not be determined.")
        case .rangingUnavailable:
            print("rangingUnavailable: ranging is disabled.")
        case .rangingFailure:
            print("rangingFailure: a general ranging error occurred.")
        default : break
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-02-26
      • 1970-01-01
      • 1970-01-01
      • 2015-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-19
      • 2012-12-03
      相关资源
      最近更新 更多