NSRangeException在index中的对象中随机崩溃应用程序

时间:2015-05-13 12:47:27

标签: crash

NSDictionary * latestCollection = [json objectWithString:responseString error:& error];

NSDictionary *data=[latestCollection valueForKey:@"results"];
if([data count]>0 && data!=nil)
{
    arrayAddress=[[NSMutableArray alloc]initWithArray:[data valueForKey:@"formatted_address"]];
    /*********Showing address of new location******/
    [lblAddress setText:[NSString stringWithFormat:@"%@",[arrayAddress objectAtIndex:0]]];
    [lblPickUpAddress setText:[NSString stringWithFormat:@"%@",[arrayAddress objectAtIndex:0]]];
}
else
{
    NSLog(@"Address not available");
}

1 个答案:

答案 0 :(得分:-1)

您的问题是您的if子句:

if([data count]>0 && data!=nil)

if将从左到右进行评估,首先,它会访问data的{​​{1}},但它可能是count,所以只需切换表达式就像这样:

nil

如果其中一个表达式为if( data!=nil && [data count]>0) ,则if子句将立即停止,因此它将对false进行检查,如果其为nil,则它将访问数据以获得计数。如果nildata,则无法访问nil。这就是为什么你的应用程序崩溃。