gpt4 book ai didi

ios - 使用 Geocoder 更新属性以获取用户实际地址,EXC_BAD_ACCESS

转载 作者:行者123 更新时间:2023-11-29 12:59:48 25 4
gpt4 key购买 nike

我是一个相对较新的 iOS 开发人员,我正在处理地理编码器完成处理程序,我不明白如何使用地理编码器找到的地址更新变量并用它做一些事情。简而言之,我希望地理编码器 block 以某种方式完成使用信息

- (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];

}

我已经尝试过调度主队列,但我无法获取注释并在方法中使用它,在网络上进行了深入搜索后,我对线程、队列和那里的执行顺序有点困惑。当运行该方法时,它会在 block 后记录注释时抛出异常 EXC_BAD_ACCESS....

最佳答案

reverseGeocodeLocation 方法异步运行,因此您不应在 completionHandler block 之外引用 annotationannotation 字符串在您到达 reverseGeocodeLocation block 之后的代码时尚未设置。

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

- (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];
});
}
} ];
}

关于ios - 使用 Geocoder 更新属性以获取用户实际地址,EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20072609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com