Ios 从方法外部访问属性

Ios 从方法外部访问属性,ios,iphone,objective-c,Ios,Iphone,Objective C,我正在尝试查找当前城市的用户 @interface WeatherViewController () @property (strong, nonatomic) NSString *test; @end - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { [lo

我正在尝试查找当前城市的用户

@interface WeatherViewController ()
    @property (strong, nonatomic) NSString *test;
@end

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
[locationManager stopUpdatingLocation];

CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
    for (CLPlacemark * placemark in placemarks) {
        _test = [placemark locality];
        NSLog(@"%@", _test);
    }
}];
}
运行应用程序会根据位置记录当前城市


如果我尝试运行NSLog@%@,请执行测试;在方法locationManager之外,重新运行的数据为null。有人能解释一下为什么_test只在for循环中返回一个值吗?

您应该使用dotSyntax,self.test=[placeMark locality]
很可能这是一个内存管理问题

这是一个异步方法问题,而不是内存管理问题。愚蠢的我,这是完全正确的_在找到位置之前不会设置测试:-因为测试在块完成后释放。