Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

swift - CLGeocoder().geocodeAddressString error codes

I am working on a project where I use CLGeocoder to determine the placemarks for a particular location. Very simple call like this.

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

Instead of printing out the NSError's localizedDescription, I would like to be able to capture the CLError code and respond accordingly. For example, if the location can't be found, my localizedDescription prints

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

How can I, in Swift determine the CLError code so that I do not have to switch on some localized description that will change based on user's locale? Can this be done? I only have a couple of cases that I want to switch on.

question from:https://stackoverflow.com/questions/65837006/clgeocoder-geocodeaddressstring-error-codes

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to cast the error as CLError and then switch the error 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
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...