次の関数の中で、私はブロックを使用しました。しかし、この関数を呼び出すと、ブロックが実行される前でも返されます。Block inturnはスレッドを使用して個別に実行するため、関数はスレッドが戻るのを待たないことを理解しました。しかし、関数の実行を待機させる他の方法、またはブロック自体を使用せずにこのブロックの機能を実現する他の方法はありますか?
-(int)findCurrentZip
{
CLLocation *userLocation = [[CLLocation alloc] initWithLatitude:[self findCurrentLatitude]
longitude:[self findCurrentLongitude]];
int zipcode;
self.myGeocoder = [[CLGeocoder alloc] init];
[self.myGeocoder
reverseGeocodeLocation:userLocation
completionHandler: (id)^(NSArray *placemarks, NSError *error) {
if (error == nil && [placemarks count] > 0)
{
NSLog(@"Placemarks: %@",placemarks);
CLPlacemark *placemark = [placemarks objectAtIndex:0];
NSLog(@"Country = %@", placemark.country);
NSLog(@"Postal Code = %@", placemark.postalCode);
zipcode = (int)placemark.postalCode;
NSLog(@"Locality = %@", placemark.locality);
NSLog(@"Country%@",[placemarks lastObject]);
}
else if (error == nil && [placemarks count] == 0)
{
NSLog(@"No results were returned.");
}
else if (error != nil)
{
}
}];
return zipcode;
}