在ios中使用__block和CLGecoder的问题?

时间:2012-12-18 12:49:32

标签: objective-c ios objective-c-blocks clgeocoder

我编写了CLGecoder的代码,代码是

-(NSString *)GetCurrentAddress:(CLLocation *)Location
{
  __block NSString  *locatedaddress;
    CLGeocoder *Gecoder=[[CLGeocoder alloc]init];
    [Gecoder reverseGeocodeLocation: Location completionHandler: 
     ^(NSArray *placemarks, NSError *error) {

         //Get address
         CLPlacemark *placemark = [placemarks objectAtIndex:0];

         NSLog(@"Placemark array: %@",placemark.addressDictionary );

         //String to address
       locatedaddress = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];

         //Print the location in the console
         NSLog(@"Currently address is: %@",locatedaddress);


     }];
    return locatedaddress;
}

问题是找到内部的完成握手有值但外面不是吗? 任何人都可以帮助我吗? 在此先感谢。

1 个答案:

答案 0 :(得分:3)

地理编码器是异步的 - 根据定义,您无法立即获得结果。您必须使用完成处理程序来执行您想要对结果执行的任何操作。