为什么这个CLLocationCoordinate2D变量不可分配? [英] Why is this CLLocationCoordinate2D variable unassignable?
问题描述
我有一个地理编码方法,我想让它返回它为我生成的CLLocationCoordinate2D。
I have a geocoder method and I want it to return the CLLocationCoordinate2D that it generates for me.
- (CLLocationCoordinate2D)geocode{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(0,0);
[geocoder geocodeAddressDictionary:self.placeDictionary completionHandler:^(NSArray *placemarks, NSError *error) {
if([placemarks count]) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
CLLocation *location = placemark.location;
coordinate = location.coordinate;
} else {
NSLog(@"error");
}
}];
return coordinate;
}
行 coordinate = location.coordinate $但是,c $ c>会产生错误。 XCode说
坐标
是一个不可分配的变量。有谁看到我做错了什么?
The line coordinate = location.coordinate
produces an error however. XCode says coordinate
is an unassignable variable. Anyone see what I'm doing wrong?
更新:
按照sebastian的建议,我得到了编译的代码,但是,坐标
未正确设置。如果你看看我在方法中放入的两个NSLog语句,第一个打印出我需要分配给 coordinate
的正确坐标,但是一旦if语句退出时,坐标
返回设置为(0,0)。第二个NSLog语句打印(0,0)。有谁知道我怎么解决这个问题?
After following sebastian's advice, I got the code to compile, however, coordinate
is not being properly set. If you take a look at both of the NSLog statements i put in the method, the first one prints out the correct coordinates that I need assigned to coordinate
, however as soon as the if statement exits, coordinate
goes back to being set to (0,0). The second NSLog statement prints (0,0). Anyone know how I can fix this?
- (CLLocationCoordinate2D)geocode{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
__block CLLocationCoordinate2D geocodedCoordinate = CLLocationCoordinate2DMake(0,0);
[geocoder geocodeAddressDictionary:self.placeDictionary completionHandler:^(NSArray *placemarks, NSError *error) {
if([placemarks count]) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
CLLocation *location = placemark.location;
geocodedCoordinate = location.coordinate;
NSLog(@"%f, %f", coordinate.longitude, coordinate.latitude);
} else {
NSLog(@"error");
}
}];
NSLog(@"%f, %f", coordinate.longitude, coordinate.latitude);
return coordinate;
}
推荐答案
你必须使用 __ block
关键字如果要分配给在块范围之外定义的变量:
You have to use the __block
keyword if you want to assign to a variable that was defined outside the block's scope:
__block CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(0,0);
看看块和 = http://developer.apple.com/library/ios/documentation/cocoa/Conceptual/Blocks/Articles/00_Introduction.html =nofollow> Apple的块编程主题
Have a look at the Blocks and Variables section of Apple's Block Programming Topics
关于编译时未设置的变量:
geocodeAddressDictionary:completionHandler:
以异步方式运行。这意味着它会立即返回,但是当结果可用时,块会在稍后执行。您需要更改调用方法的方式。目前你可能正在做这样的事情
geocodeAddressDictionary:completionHandler:
runs asynchronously. That means it returns immediately but the block gets executed later, when the results are available. You need to change the way you call your methods. At the moment you are probably doing something like this
self.myCoordinate = [self geocode];
你需要做的更像是:
- (void)geocode{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressDictionary:self.placeDictionary completionHandler:^(NSArray *placemarks, NSError *error) {
if([placemarks count]) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
CLLocation *location = placemark.location;
self.myCoordinate = location.coordinate;
NSLog(@"%f, %f", coordinate.longitude, coordinate.latitude);
} else {
NSLog(@"error");
}
}];
}
运行 [self geocode]
立即返回,并且当块运行时将设置 myCoordinate
。
Running [self geocode]
returns immediately and myCoordinate
will be set when the block runs.
如果你这样做这样,请注意这可能会导致参考周期,因为自我被块保留。
这篇关于为什么这个CLLocationCoordinate2D变量不可分配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!