The following class provides a mutating function to change its property:
class Person {
struct Location {
var coordinate: CLLocationCoordinate2D!
var city: String?
mutating func setLocationNameFromCoordinate(completion:(()->())?) {
let location = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
CLGeocoder().reverseGeocodeLocation(location) { (placemarks: [CLPlacemark]?, error: NSError?) in
guard let city = placemarks?.first?.locality where error == nil else {
return
}
self.city = city //1
completion?()
}
}
}
var location: Location?
}
The function is called like so:
person.location?.setLocationNameFromCoordinate() {
print(person.location?.city) //2
}
However, at 1
the city name is set, looking at it from inside the location Struct, but at 2
the city name is not set for the object. What am I doing wrong here?