I'm try to localize a start and end point to an address string, so that I can store it into NSUserDefaults
. The problem is that the method continues executing and does not set my variable.
NSLog(@"Begin");
__block NSString *returnAddress = @"";
[self.geoCoder reverseGeocodeLocation:self.locManager.location completionHandler:^(NSArray *placemarks, NSError *error) {
if(error){
NSLog(@"%@", [error localizedDescription]);
}
CLPlacemark *placemark = [placemarks lastObject];
startAddressString = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@",
placemark.subThoroughfare, placemark.thoroughfare,
placemark.postalCode, placemark.locality,
placemark.administrativeArea,
placemark.country];
returnAddress = startAddressString;
//[self.view setUserInteractionEnabled:YES];
}];
NSLog(returnAddress);
NSLog(@"Einde");
This is what my application debugger shows:
start
einde
If for example the address of my location is : "Mainstreet 32, CITY". Then what i would want to see is the following:
Start
Mainstreet 32, CITY
Einde
The problem is that my code does not wait for my CLGeocoder
to finish, so my variable returnAddress
is not set when it is returned, and it is empty.
Does anyone know how to work around this?