【问题标题】:'NSInvalidArgumentException', reason: '-[NSNull objectAtIndex:]: unrecognized selector sent to instance [duplicate]'NSInvalidArgumentException',原因:'-[NSNull objectAtIndex:]:无法识别的选择器发送到实例 [重复]
【发布时间】:2014-01-08 04:18:40
【问题描述】:

我正在使用核心定位框架,它工作正常,但有时会出现异常

'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;
}

【问题讨论】:

  • 那是因为您的“valueForKey”之一为空...
  • 但 avi 有时可以正常工作..有时会出现异常
  • 那么你必须先检查:if (![yourProp isKindOfClass:[NSNull class]])
  • 在所有情况下都需要验证valueForKey是否为数组。记录值以检查是否相同。
  • 您也没有将arrdata 设置为属性

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


【解决方案1】:

我检查了 url 的响应&我认为你误解了使用 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];          
}

.....

【讨论】:

  • IOS 不会将 NULL 视为 nil,并且在运行时它会崩溃。要解决此问题,只需将您的 json 字符串与 NSNULL 进行比较,然后分配给字符串变量 NSString *yourVariable = ([stringVariable isEqual: [NSNull null]]) ? @"" : 字符串变量;
猜你喜欢
  • 1970-01-01
  • 2023-03-11
  • 2015-08-02
  • 1970-01-01
  • 2015-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多