So my question is this: I'm trying to implement a extension to CLGeoCoder to be able to get a city name from a CLLocation but I'm facing a problem when I try to set the inout city name string inside the completionhandler of the reverseGeocodeLocation
Here is my code for my extension:
extension CLGeocoder {
func findCityName(location:CLLocation, inout cityName:String) -> Void{
self.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
// Place details
var placeMark: CLPlacemark!
placeMark = placemarks?[0]
// Address dictionary
print(placeMark.addressDictionary)
// City
if let city = placeMark.addressDictionary!["City"] as? String {
cityName = city
}
})
}
}
The goal of this extension is to be able to set a string with a city name by passing it by reference to this function.
An example would be:
var cityName = ""
let location:CLLocation(latitude: 1, longitude: 1)
let geocoder: CLGeocoder()
geocoder.findCityName(location, &cityName)
///Later on in the process I would want to be able to use cityName for different purposes in my code
The problem I'm facing is that after setting the cityName reference in the completionHandler, the value of cityName stays empty outside of the completionhandler.
I'm new to swift so I've maybe missed something on pointers or how to use them but I had the understanding that inout would make it possible to change the value of a variable from inside a function or a closure/completionhandler.
Thank you in advance for any information you could give me on this problem I'm facing.
Martin