您将如何切换 Swift 中的 kCL 错误?

How would you switch through kCL error in Swift?

您如何在 Swift 中切换 kCL 错误?

我下面的尝试是 Use of unresolved identifier 'kCLErrorGeocodeFoundNoResult':

switch error {
case kCLErrorGeocodeFoundNoResult:  print("\(#function): No results")
default: print("\(#function): Error reports \(error.localizedDescription)")
}

error 必须转换为 CLError 才能访问 code 属性,如下所示:

if let error = error as? CLError {
    switch error.code {
       case CLError.Code.geocodeFoundPartialResult:  print("\(#function): No results")
       default: print("\(#function): Error reports \((error as NSError).localizedDescription)")
    }
}

示例:

    let geocoder = CLGeocoder()
    let location = CLLocation(latitude: 23.8759, longitude: 90.3795)
    geocoder.reverseGeocodeLocation(location) {placemarks, error in

        if let error = error as? CLError {
            NSLog("Geocode failed with error: \(error)")
            let message: String
            switch error.code {
            case CLError.Code.geocodeFoundNoResult:
                message = "kCLErrorGeocodeFoundNoResult"
            case CLError.Code.geocodeCanceled:
                message = "kCLErrorGeocodeCanceled"
            case CLError.Code.geocodeFoundPartialResult:
                message = "kCLErrorGeocodeFoundNoResult"
            default: message = (error as NSError).description
            }
            return
        }

    }