如何获得`placemark`属性?

时间:2015-08-18 08:38:07

标签: ios

我想获得当前位置的country属性。我已在模拟器上测试了此代码。我已将假位置设置为apple headquarter

代码:

  - (void)viewDidLoad
        {
        [super viewDidLoad];
        geo=[[CLGeocoder alloc]init];
        pm=[[CLPlacemark alloc]init];
        lm=[[CLLocationManager alloc]init];
        lm.delegate=self;
        [lm startUpdatingLocation];

        }

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *loc=[[CLLocation alloc]init];
    loc=[locations lastObject];
    [geo reverseGeocodeLocation:loc completionHandler:^(NSArray *ARR,NSError *ERRO)

    {

    if(ERRO==nil&&[ARR count]>0)
    {
    pm=[ARR objectAtIndex:0];
    NSLog(@"%@",pm.country);

    }
    else

    {
    NSLog(@"%@",ERRO.debugDescription);
    }



    }];

}

问题:它在pm=[ARR objectAtIndex:0];行显示例外情况,即exc_bad_access

代码有问题,或者我们无法在模拟器上测试它。

1 个答案:

答案 0 :(得分:1)

尝试在块中声明变量:

[geo reverseGeocodeLocation:loc completionHandler:^(NSArray *ARR, NSError *ERRO)
{
    if(ERRO == nil && [ARR count] > 0)
    {
        CLPlacemark *pm = [ARR objectAtIndex:0];
        NSLog(@"%@",pm.country);
    }
    else
    {
        NSLog(@"%@",ERRO.debugDescription);
    }
}];