【问题标题】:NSRangeException beyond bounds after calling JSON调用 JSON 后 NSRangeException 超出范围
【发布时间】:2013-07-01 21:02:02
【问题描述】:

我在我的应用程序的异步线程上调用 Google 的地理编码 API 和距离矩阵 API。但它发出 NSRangeException。 这是我的代码:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^
{
    NSString *urlStringGeoCoding;
    NSString *urlString;

    urlStringGeoCoding = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/"
                                                    @"api/geocode/json?"
                                                    @"latlng=40.714224,-73.961452&sensor=true"];

    urlString = [NSString stringWithFormat:@"http://maps.googleapis.com"
                                           @"/maps/api/distancematrix/json?"
                                           @"origins=%@&destinations=%@&mode=driving&"
                                           @"language=en&sensor=true&units=imperial","
                                           @"current,@"Chicago,IL"];

    NSURL   *urlGeo = [NSURL URLWithString:[urlStringGeoCoding
                          stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSURL   *url    = [NSURL URLWithString:[urlString
                          stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSData  *googledatageo = [NSData dataWithContentsOfURL:urlGeo];
    NSData  *googledata    = [NSData dataWithContentsOfURL:url];
    NSError *errorgeo;
    NSError *error;

    NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:googledata 
                                                                options:kNilOptions 
                                                                  error:&error];
    NSMutableDictionary *jsongeo = [NSJSONSerialization JSONObjectWithData:googledatageo 
                                                                   options:kNilOptions 
                                                                     error:&errorgeo];

    NSString *result = [[[[[[json objectForKey:@"rows"] objectAtIndex: 0] objectForKey:@"elements"] objectAtIndex:0]objectForKey:@"distance"]objectForKey:@"text"];
    if(jsongeo)
    {
       NSString *resultgeo = [[[jsongeo objectForKey:@"results"] objectAtIndex: 0] objectForKey:@"formatted_address"] ;
    }

    dispatch_sync(dispatch_get_main_queue(), ^
    {     
        cell.distanceLabel.text = result;
    });
});

这是我得到的错误: 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“* -[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array” * 首先抛出调用栈: (0x289a012 0x2210e7e 0x284fb44 0x1318c 0x57f953f 0x580b014 0x57fc2e8 0x57fc450 0x9112ae72 0x91112d2a)

这里如何防止范围异常?

【问题讨论】:

  • 请格式化您发布的代码,使用缩进等。
  • 三次objectAtIndex:0,放置断点并打印json 和jsongeo,然后告诉我你看到了什么。它可能会成为其中之一。

标签: ios nsarray


【解决方案1】:

在对它们调用 objectAtIndex: 之前,通过检查 "rows"、"elements" 和 "results" 数组的 count 来防止这些范围异常。

将这两条很长的行分开是个好主意,它们分配了result 和resultgeo。只需使用一些临时的NSArray 变量即可。

【讨论】:

    猜你喜欢
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-30
    • 2013-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多