使用Geocoder更新属性以获取用户实际地址EXC_BAD_ACCESS [英] Update property using Geocoder to get user actual address, EXC_BAD_ACCESS

查看:140
本文介绍了使用Geocoder更新属性以获取用户实际地址EXC_BAD_ACCESS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个相对较新的iOS开发人员,并且正在处理 geocoder完成处理程序,因此我不了解如何使用geocoder找到的地址来更新变量,以及如何对其进行处理。简而言之,我希望地理编码器块以某种方法使用该信息

Im relatively a new iOS developer, and im dealing with the geocoder completion handler, i do not understand how to update a variable with the address find by the geocoder and the do some stuff with it. In few words, i want that the geocoder block finishes to use the info in some method.

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

}

我尝试了调度主队列,但无法在网络上进行深入搜索后,我对线程,队列和执行顺序感到困惑,然后在方法中使用该注释。在运行该方法时,在块后记录注释时会引发异常EXC_BAD_ACCESS...。

I have tried the dispatch main queue, and i cant get the annotation and use it in the method, after a deep search around the web im a bit confused about threads, queues and the order of there execution. When running the method it throws the exception EXC_BAD_ACCESS when logging the annotation after the block....

推荐答案

reverseGeocodeLocation 方法异步运行,因此您不应在 completionHandler 注释 c>块。在 reverseGeocodeLocation 块之后,您到达该代码的位置尚未设置注释字符串。

The reverseGeocodeLocation method runs asynchronously, so you shouldn't reference the annotation outside of the completionHandler block. The annotation string has not been set by the point you get to that code after the reverseGeocodeLocation block.

因此,您可能希望将代码移至 reverseGeocodeLocation 块之后,以解决此异步问题:

Thus, you might want to move your code after the reverseGeocodeLocation block to inside it, resolving this asynchronous issue:

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

这篇关于使用Geocoder更新属性以获取用户实际地址EXC_BAD_ACCESS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆