当我尝试运行我的应用时出现此错误:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 9223372036854775807 beyond bounds [0 .. 0]'
我真的不知道我的错误是什么。我还注意到,当我在我的表中存储一个dateTime数据时,它用括号括起来,引用就像上午9:47一样(“上午9:47”)。我是Xcode的新手。我已经一周了。这是我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = @"SimpleTableCell";
SimpleTableCell *cell = (SimpleTableCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SimpleTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSInteger indexOfAlarm = [self.dbManager.arrColumnNames indexOfObject:@"Time"];
cell.timeLabel.text = [NSString stringWithFormat:@"%@",[[self.arrAlarmInfo objectAtIndex:indexPath.row]objectAtIndex:indexOfAlarm] ];
return cell;
}
答案 0 :(得分:2)
9223372036854775807的值是常量NSNotFound
。它表示搜索的实例在数组中不存在。要检查它,请添加if
- 声明:
NSInteger indexOfAlarm = [self.dbManager.arrColumnNames indexOfObject:@"Time"];
if(indexOfAlarm != NSNotFound) // <-
{
cell.timeLabel.text = [NSString stringWithFormat:@"%@",[[self.arrAlarmInfo objectAtIndex:indexPath.row]objectAtIndex:indexOfAlarm] ];
}
return cell;
如果您希望@"Time"
始终在数组中,您必须找出原因,为什么它不存在。
答案 1 :(得分:-2)
更改此代码
NSInteger indexOfAlarm = [self.dbManager.arrColumnNames indexOfObject:@"Time"];
cell.timeLabel.text = [NSString stringWithFormat:@"%@",[[self.arrAlarmInfo objectAtIndex:indexPath.row]objectAtIndex:indexOfAlarm] ];
以强>
NSNumber *indexOfAlarm = [self.dbManager.arrColumnNames indexOfObject:@"Time"];
cell.timeLabel.text = [NSString stringWithFormat:@"%@",[[self.arrAlarmInfo objectAtIndex:indexPath.row]objectAtIndex:indexOfAlarm.intValue] ];