swift - CLGeocoder()。geocodeAddressString错误代码

标签 swift xcode error-handling clgeocoder

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

CLGeocoder().geocodeAddressString(location) { (placemarks, error) in
            if let error = error {
                print(error.localizedDescription)
            }
我希望能够捕获CLError代码并做出相应的响应,而不是打印出NSError的localizedDescription。例如,如果找不到该位置,则会打印我的localizedDescription
The operation couldn’t be completed. (kCLErrorDomain error 8.)
在Swift中,如何确定CLError代码,这样我就不必打开将根据用户区域设置更改的某些本地化描述?
能做到吗?
我只想打开几个案例。

最佳答案

您需要将错误转换为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
    }
}

关于swift - CLGeocoder()。geocodeAddressString错误代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65837006/

相关文章:

ios - Swift 停止卸载表格 View 单元格?

ios - 将撤消管理器与 mac 催化剂一起使用

php - htaccess ErrorDocument 404重定向不起作用

ios - 需要在所有 VC 中添加通用的 UIBarButtonItem

ios - 单元测试未编译 - Swift Xcode 9.2

ios - 如何以编程方式将视频从 iPhone 应用程序上传到 youtube?

已安装 xcode 但找不到 macOS_SDK_headers_for_macOS_10.14.pkg

f# - 使用 FSyacc 解析期间的有意义的错误

actionscript-3 - flex/air ActionScript 错误的事件监听器

swift - ARKit – 将 OBJ 或 DAE 转换为 arobject