Objective-C变量超出块

时间:2014-10-19 08:11:32

标签: ios objective-c

我正在努力将变量从块中移除。似乎是一个非常基本的东西,但我无法弄清楚!我该如何访问它?例如挡出usercity? Usercity在.h。

中声明为NSString
[ceo reverseGeocodeLocation: loc completionHandler:
 ^(NSArray *placemarks, NSError *error) {


     CLPlacemark *placemark = [placemarks objectAtIndex:0];
     //NSLog(@"placemark %@",placemark);
     //String to hold address
     //NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
     //NSLog(@"addressDictionary %@", placemark.addressDictionary);

     //NSLog(@"placemark %@",placemark.region);
     NSLog(@"Land %@",placemark.country);  // Give Country Name
     NSLog(@"City %@",placemark.locality); // Extract the city name
     NSLog(@"Adresse %@",placemark.name);
     //NSLog(@"location %@",placemark.ocean);
     NSLog(@"Zip %@",placemark.postalCode); //ZipCode
     NSLog(@"sonstiges %@",placemark.subLocality);


     //Save values in variables
     usercountry = placemark.country;
     usercity = placemark.locality;
     userzip = placemark.postalCode;
     NSLog(@"usercity: %@",usercity);

     //NSLog(@"location %@",placemark.location);



 }

 ];

2 个答案:

答案 0 :(得分:1)

这是你正在寻找的吗?

__block NSString *userCity;
[ceo reverseGeocodeLocation: loc completionHandler:^(NSArray *placemarks, NSError *error) {

     CLPlacemark *placemark = [placemarks objectAtIndex:0];
     ...
     userCity = placemark.locality;

}];

但是如果你想实际能够在块之外检查它的值,那么在完成处理程序更新值之后你必须这样做。也许使它成为一种财产,即。 self.userCity

答案 1 :(得分:0)

块中的代码必须将usercity存储在您想要的位置。你不能“阻止”一个块,块中的代码必须这样做。

你知道一个块可以访问周围方法中的所有变量,不是吗?