'NSInvalidArgumentException',原因:' - [NSNull objectAtIndex:]:无法识别的选择器发送到实例

时间:2013-12-19 14:34:42

标签: ios iphone objective-c cocoa-touch core-location

我使用的是重置框架,它工作正常,但有时它会像

一样出现异常
  

'NSInvalidArgumentException',原因:-[NSNull objectAtIndex:]:无法识别的选择器发送到实例

这是我的代码:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    currentLocation = newLocation;

    if (currentLocation != nil) {
        NSString *strLink1=[NSString stringWithFormat:@"http://ajax.googleapis.com/ajax/services/search/local?v=1.0&q=restaurents&rsz=8&sll=%f,%f&radius=1000&output=json",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude];
        NSURL *url1=[NSURL URLWithString:strLink1];
        NSData *data1=[NSData dataWithContentsOfURL:url1];
        NSError *err;
        response1=[NSJSONSerialization JSONObjectWithData:data1 options:kNilOptions error:&err];
        arrdata=[[NSMutableArray alloc]init];

        for (int i=0; i<=7; i++) {
            NSString *strTitle=[[[[response1 valueForKey:@"responseData"]valueForKey:@"results"]objectAtIndex:i ]valueForKey:@"title"];
            NSString *strPhone=[[[[[[response1 valueForKey:@"responseData"]valueForKey:@"results"]objectAtIndex:i ]valueForKey:@"phoneNumbers"]objectAtIndex:0]valueForKey:@"number"];
            NSString *strAdd1=[[[[[response1 valueForKey:@"responseData"]valueForKey:@"results"]objectAtIndex:i ]valueForKey:@"addressLines"]objectAtIndex:0];
            NSString *strAdd2=[[[[[response1 valueForKey:@"responseData"]valueForKey:@"results"]objectAtIndex:i ]valueForKey:@"addressLines"]objectAtIndex:1];
            NSString *strAdd=[NSString stringWithFormat:@"%@,%@",strAdd1,strAdd2]; 
            NSString *strLat=[[[[response1 valueForKey:@"responseData"]valueForKey:@"results"]objectAtIndex:i ]valueForKey:@"lat"];
            NSString *strLng=[[[[response1 valueForKey:@"responseData"]valueForKey:@"results"]objectAtIndex:i ]valueForKey:@"lng"];

            dictdata1=[[NSMutableDictionary alloc]initWithObjectsAndKeys:strTitle,@"0",strAdd,@"1",strLat,@"2",strLng,@"3",strPhone,@"4", nil];
            [arrdata addObject:dictdata1];          
        }
    }
    [tblView reloadData];   
}  

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *strCell=@"cellIdentifier";
    UITableViewCell *cell=[tblView dequeueReusableCellWithIdentifier:strCell];
    UILabel *lblTitle;
    UILabel *lblAdd;
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strCell];
        lblTitle=[[UILabel alloc]initWithFrame:CGRectMake(0,0,300, 50)];
        lblAdd=[[UILabel alloc]initWithFrame:CGRectMake(0,20, 320, 50)];
        [cell.contentView addSubview:lblTitle];
        [cell.contentView addSubview:lblAdd];         
    }

    lblTitle.backgroundColor=[UIColor clearColor];
    lblTitle.text=[[arrdata objectAtIndex:indexPath.row]valueForKey:@"0"];

    lblAdd.backgroundColor=[UIColor clearColor];
    lblAdd.text=[[arrdata objectAtIndex:indexPath.row]valueForKey:@"1"]; 

    return cell;
}

1 个答案:

答案 0 :(得分:4)

我检查网址和网址的响应我认为你误解了使用jsonObject的方法

首先,崩溃的原因是因为您要获取的某些值是类 NSNull ,而不是您想要的 NSArray

第二,你无法确定可以获得的结果,因此你无法将结果的数量修改为 7

首先,您应确保不会获得 NSNull 对象,因此请将 NSDictionary

分类
- (id)objectForKeyNotNull:(id)key {
    id object = [self objectForKey:key];
    if (object == [NSNull null])
        return nil;

    return object;
}

然后,改变使用JsonObject的方式

......

response1=[NSJSONSerialization JSONObjectWithData:data1 options:kNilOptions error:&err];
arrdata=[[NSMutableArray alloc]init];

for ( NSDictionay *result in response[@"responseData"][@"results"]) {
    NSString *strTitle=[result objectForKeyNotNull:@"title"];
    NSString *strPhone=[result objectForKeyNotNull:@"number"];
    NSString *strLat=[result objectForKeyNotNull:@"lat"];
    NSString *strLng=[result objectForKeyNotNull:@"lng"];

    for ( NSString* address in [result objectForKeyNotNull:@"addressLines"])
    {
        //......
    }

    ......


    dictdata1=[[NSMutableDictionary alloc]initWithObjectsAndKeys:........];


    [arrdata addObject:dictdata1];          
}

.....