我正在使用CLGeocoder来确定特定位置的地标的项目。
这样非常简单的调用。
CLGeocoder().geocodeAddressString(location) { (placemarks, error) in
if let error = error {
print(error.localizedDescription)
}
我希望能够捕获CLError代码并做出相应的响应,而不是打印出NSError的localizedDescription。例如,如果找不到该位置,则会打印我的localizedDescriptionThe 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/