【发布时间】: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