【问题标题】:CLGeocoder returns (null) in a NSString but not in NSLogCLGeocoder 在 NSString 但不在 NSLog 中返回 (null)
【发布时间】:2014-01-27 00:03:35
【问题描述】:

我对@9​​87654322@ 有疑问,如果我尝试使用CLGeocoder 获取地址名称并且用户当前坐标正确返回NSLog 中的地址名称、邮政编码、州等,但如果我尝试在NSString 中发送这些信息[包含在NSMutableArray 中,但我认为这不是问题] 它返回(null) 值。我怀疑这个问题是由我在代码中使用的块引起的,但我不确定,我也不知道如何解决这个问题。

这是我的代码:

- (NSArray *)dataArrayPosizione
{
    if (self.arrayPosizione == nil)
    {

        reverseGeocoder = [[CLGeocoder alloc] init];

        locationToReverseGeocode = [[CLLocation alloc]initWithLatitude:self.mapView.userLocation.location.coordinate.latitude longitude:self.mapView.userLocation.location.coordinate.longitude];

        [reverseGeocoder reverseGeocodeLocation:locationToReverseGeocode completionHandler:^(NSArray *placemarks, NSError *error)
        {
            if (placemarks && placemarks.count > 0)
            {
                placemark = placemarks[0];

                NSLog(@"\n%@ %@\n%@, %@, %@\n%@", [placemark subThoroughfare],[placemark thoroughfare], [placemark locality], [placemark administrativeArea], [placemark postalCode], [placemark country]);
            }
        }];

        informazioniPosizione = [NSMutableArray new];

        [informazioniPosizione addObject:[NSString stringWithFormat:@"Indirizzo: %@", [placemark country]]];
        [informazioniPosizione addObject:[NSString stringWithFormat:@"Latitudine: %g\u00B0\nLongitudine: %g\u00B0", self.mapView.userLocation.location.coordinate.latitude, self.mapView.userLocation.location.coordinate.longitude]];

        self.arrayPosizione = informazioniPosizione;
    }

    return self.arrayPosizione;
}

还有一张图片:

有人可以帮我解决这个问题吗?如果发布的代码不足以让您理解问题,我将粘贴更多行。

谢谢

【问题讨论】:

    标签: ios objective-c objective-c-blocks cllocation clgeocoder


    【解决方案1】:

    您正在地理编码块执行之前创建字符串。它应该是这样的:

    - (NSArray *)dataArrayPosizione
    {
        if (self.arrayPosizione == nil)
        {
    
            reverseGeocoder = [[CLGeocoder alloc] init];
    
            locationToReverseGeocode = [[CLLocation alloc]initWithLatitude:self.mapView.userLocation.location.coordinate.latitude longitude:self.mapView.userLocation.location.coordinate.longitude];
    
            [reverseGeocoder reverseGeocodeLocation:locationToReverseGeocode completionHandler:^(NSArray *placemarks, NSError *error)
            {
                informazioniPosizione = [NSMutableArray new];
    
                if (placemarks && placemarks.count > 0)
                {
                    placemark = placemarks[0];
    
                    NSLog(@"\n%@ %@\n%@, %@, %@\n%@", [placemark subThoroughfare],[placemark thoroughfare], [placemark locality], [placemark administrativeArea], [placemark postalCode], [placemark country]);
    
                    [informazioniPosizione addObject:[NSString stringWithFormat:@"Indirizzo: %@", [placemark country]]];
                    [informazioniPosizione addObject:[NSString stringWithFormat:@"Latitudine: %g\u00B0\nLongitudine: %g\u00B0", self.mapView.userLocation.location.coordinate.latitude, self.mapView.userLocation.location.coordinate.longitude]];
    
                }
    
                self.arrayPosizione = informazioniPosizione;
    
                return self.arrayPosizione;
            }];
    
        }
    
    }
    

    【讨论】:

    • 您好@bachonk,我已经尝试过您的代码,但结果如下:-/ i.imgur.com/kCo7ZvQ.png 无论如何感谢您的回答:-)
    • 您在块执行之前返回了一个空数组。我相应地更新了我的代码,但请注意,这不是最推荐的处理方式。
    • 不,我应该放什么代码以及在哪里刷新我的UITableView?感谢您的帮助和时间 :-)
    • 假设您的表格视图在其数据源中使用self.arrayPosizione,您需要在self.arrayPosizione = informazioniPosizione; 行中设置该值后立即调用[tableView reloadData]
    【解决方案2】:

    CLGeocoder 的块将异步执行,因此当您尝试使用地标变量时尚未设置它。就像 bachonk 建议的那样,在地理编码器的块内移动代码。将地标的信息添加到数组后,您只需在 tableView 上调用 -[UITableView reloadData],信息就会出现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-14
      • 2021-12-13
      相关资源
      最近更新 更多