我正在做一个项目,在这个项目中,我使用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代码,这样我就不必打开一些本地化的描述,这些描述将根据用户的区域设置进行更改?这可以做到吗?我只有几个案子想换。
发布于 2021-01-22 07:15:49
您需要将错误类型转换为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
}
}https://stackoverflow.com/questions/65837006
复制相似问题