我有一个看起来像这样的方法:
-(NSString *)getCityFromLocation:(CLLocation *)location {
__block NSString *city;
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation: location completionHandler:
^(NSArray *placemarks, NSError *error) {
//Get address
CLPlacemark *placemark = [placemarks objectAtIndex:0];
city = [placemark.addressDictionary objectForKey:@"City"];
NSLog(@"city 1: %@", city);
}];
return city;
}
我这样称呼它:
NSString *city = [self getCityFromLocation:currentLocation];
NSLog(@"city 2: %@", city);
在 NSLog 中,我得到:
city 2: (null)
city 1: London
问题很明显——它在运行块之前就返回了。我怎样才能让它按预期工作,它可以返回块产生的值?