gpt4 book ai didi

swift - CLGeocoder()。geocodeAddressString错误代码

转载 作者:行者123 更新时间:2023-12-03 08:16:42 24 4
gpt4 key购买 nike

我正在使用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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com