使用Geocoder更新属性来获取用户实际地址EXC_BAD_ACCESS

我相对是一个新的iOS开发人员,即时通讯处理地理编码器完成处理程序 ,我不明白如何更新地址parsing器的地址find一个variables,并做一些与它的东西。 总之, 我希望geocoder块在某些方法中完成使用信息

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { NSLog(@"didUpdateToLocation: %@", newLocation); CLLocation *currentLocation = newLocation; if (currentLocation != nil) { longitudeLabel = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]; latitudeLabel = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]; } [locationManager stopUpdatingLocation]; // Reverse Geocoding NSLog(@"Resolving the Address"); __block NSString * **annotation**; [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) { NSLog(@"Found placemarks: %@, error: %@", placemarks, error); if (error == nil && [placemarks count] > 0) { placemark = [placemarks lastObject]; NSLog(@"location %@",addressLabel);*/ **annotation**=[NSString stringWithFormat:@" %@, %@, %@", placemark.locality, placemark.administrativeArea, placemark.country]; } } ]; NSLog(@"location %@",annotation); NSString *text = self.toPostCell.postText.text; UIImage *image = self.toPostCell.selectedImage.image; [Persistence addPostToServerAddtext:text addimage:image addbeach:nil **location:annotation**]; [NSThread sleepForTimeInterval:5.0]; self.posts = [Persistence getPostsUpTo: self.cantPosts forBeach:nil]; imageLoaded = NO; [self.tableView reloadData]; } 

我已经尝试了调度主队列,我不能得到的注释和使用它的方法,深入search周围的networking后,有点困惑线程,队列和执行的顺序。 当运行该方法时,在块之后logging注释时抛出exceptionEXC_BAD_ACCESS。

reverseGeocodeLocation方法asynchronous运行,所以不应该在completionHandler块之外引用annotation 。 在reverseGeocodeLocation块之后, annotationstring还没有被设置到那个代码的reverseGeocodeLocation

因此,您可能希望将您的代码在reverseGeocodeLocation块之后移动到其中,以解决此asynchronous问题:

 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { NSLog(@"didUpdateToLocation: %@", newLocation); CLLocation *currentLocation = newLocation; if (currentLocation != nil) { longitudeLabel = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]; latitudeLabel = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]; } [locationManager stopUpdatingLocation]; // Reverse Geocoding NSLog(@"Resolving the Address"); [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) { NSLog(@"Found placemarks: %@, error: %@", placemarks, error); if (error == nil && [placemarks count] > 0) { placemark = [placemarks lastObject]; NSLog(@"location %@",addressLabel);*/ NSString *annotation = [NSString stringWithFormat:@" %@, %@, %@", placemark.locality, placemark.administrativeArea, placemark.country]; NSLog(@"location %@",annotation); NSString *text = self.toPostCell.postText.text; UIImage *image = self.toPostCell.selectedImage.image; [Persistence addPostToServerAddtext:text addimage:image addbeach:nil location:annotation]; // I'm not sure why you're sleeping 5 seconds, but if you really want to do // something five seconds later you should probably `dispatch_after`, thus // instead of: // // [NSThread sleepForTimeInterval:5.0]; // // you might want to do the following: double delayInSeconds = 5.0; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ self.posts = [Persistence getPostsUpTo: self.cantPosts forBeach:nil]; imageLoaded = NO; [self.tableView reloadData]; }); } } ]; } 

基本上没有运行你的应用程序,你正在尝试logging你的位置,但不能保证块已经运行,然后调用“NSLog”

如果你希望日志不崩溃,就这样在同一个块内部执行;

 [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) { NSLog(@"Found placemarks: %@, error: %@", placemarks, error); if (error == nil && [placemarks count] > 0) { placemark = [placemarks lastObject]; NSLog(@"location %@",addressLabel);*/ **annotation**=[NSString stringWithFormat:@" %@, %@, %@", placemark.locality, placemark.administrativeArea, placemark.country]; NSLog(@"location %@",annotation); } } ]; 

通过这种方式,您可以确保在logging之前设置“注释”,并避免引用空指针引起的EXC_BAD_ADDRESS。