I don’t think I understand how blocks work in this particular scenario. I am trying to get the location from CLGeocoder and save MKPlacemark after block completion. So in this method:
- (MKPlacemark *)placeMarkFromString:(NSString *)address {
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
__block MKPlacemark *place;
[geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
[placemarks enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"%@", [obj description]);
}];
if (placemarks && [placemarks count] > 0) {
CLPlacemark *topResult = [placemarks objectAtIndex:0];
place = [[MKPlacemark alloc] initWithPlacemark:topResult];
[self.mapView addAnnotation:place];
}
if (error) {
NSLog(@"Error: %@", [error localizedDescription]);
}
}];
NSLog(@"%@", [place description]);
return place;
}
When I run my code, the MKPlacemark place is added to the map. However, if I register a value, it is NULL. I think this may be due to the fact that the block is not executed immediately? That way, my NSLog can be executed first, and then executeHandler is executed. However, how do I return MKPlacemark from this method so that I can use this value elsewhere in my code? thank.
source
share