Search code examples
iosswiftclgeocoderclplacemark

Swift 5 reverseGeocodeLocation() result in deprecated addressDictionary


I'm using a CLGeoCoder instance to get the city and country of a given set of coordinates (latitude and longitude) through its method reverseGeocodeLocation().

The documentation specifies that this data should be in CLPlacemark's country and locality properties but the results only seem to be inside addressDictionary property which was deprecated since iOS 11.

Image of debugging instance showing addressDictionary present but neither country nor locality

This therefore works but shows this warning on all three uses of addressDictionary: 'addressDictionary' was deprecated in iOS 11.0: Use @properties

let geocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(location, preferredLocale: nil) { (placemarksArray, error) in
    if let error = error {
        print(error)
    } else {
        if let placemark = placemarksArray?.last {
            var city = ""
            if let ct = placemark.addressDictionary?["City"] {
                city = "\(ct), "
            }
            var state = ""
            if let st = placemark.addressDictionary?["State"] {
                state = "\(st), "
            }
            var country = ""
            if let cot = placemark.addressDictionary?["Country"] {
                country = "\(cot)"
            }
            self.cityCountry = "\(city)\(state)\(country)"
        }
    }
}

I tried referencing the properties but they just return nil values.

Am I calling the wrong method or is this a bug?


Solution

  • Suddenly references to the country, locality and subAdministrativeArea started working! Couldn't figure out what caused them not to work earlier though.