我认为我不明白在这种情况下块是如何工作的。我正在尝试从 CLGeocoder 获取位置并在块完成后保存 MKPlacemark。所以在这个方法中:
- (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]);
}];
// Check for returned placemarks
if (placemarks && [placemarks count] > 0) {
CLPlacemark *topResult = [placemarks objectAtIndex:0];
// Create an MKPlacemark and add it to the mapView
place = [[MKPlacemark alloc] initWithPlacemark:topResult];
[self.mapView addAnnotation:place];
}
if (error) {
NSLog(@"Error: %@", [error localizedDescription]);
}
}];
NSLog(@"%@", [place description]);
return place;
}
当我运行我的代码时,MKPlacemark 位置确实会添加到地图中。但是,如果我记录该值,则它为 NULL。我认为这可能是因为该块没有立即执行,对吧?所以我的 NSLog 可能会先执行,然后完成处理程序运行。但是,我将如何从该方法返回 MKPlacemark,以便可以在代码的其他地方使用该值?谢谢。