[__NSArrayM objectAtIndex:]:索引517431040超出界限[0 .. 48]

时间:2013-06-12 09:34:09

标签: ios objective-c arrays

我喜欢这个错误here,但得出的结论是错误发生是因为数组是空的。然而,我的阵列不是空的。

-(void)setNumberInTippfield:(NSNumber *)aNumber{
  NSNumber *index = [self.ticket.currentSlip intValue]-1;
  NSLog(@"index: %i",index);//prints 0, expected
  Slip *slip = [self.slipArray objectAtIndex:index];
  NSNumber *number = [NSNumber numberWithInt:[aNumber intValue]-1 ];
  NSLog(@"number: %@",number);//prints out number, e.g. 25, as expected
  NSMutableArray *tNumberArray = [[NSMutableArray alloc] init];
  tNumberArray= [NSMutableArray arrayWithArray:slip.numberArray];
  //after this line, debugger tells me the array has 49 objects, as expected
  [[tNumberArray objectAtIndex:number] setSelected:YES];//this line throws exception
  //this line seems to get executed 3 times, as I can see from the breakpoints
}

之后会引发异常:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -
[__NSArrayM objectAtIndex:]: index 517431040 beyond bounds [0 .. 48]'

我认为number拥有地址而不是值,但NSLog告诉我正确的值。那个提示有什么提示吗?非常感谢。

1 个答案:

答案 0 :(得分:2)

objectAtIndex:需要int,而不是NSNumber

// ...
Slip *slip = [self.slipArray objectAtIndex:[index intValue]];

编辑:

以下一行也提出了一个问题:

NSNumber *index = [self.ticket.currentSlip intValue]-1;

结果不应该是int吗?这将解决随后出现的问题。