-1

我喜欢这个错误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 告诉我正确的值。关于那个有任何提示吗?提前非常感谢。

4

1 回答 1

2

objectAtIndex:需要一个int,而不是一个NSNumber

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

编辑:

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

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

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

于 2013-06-12T09:36:19.493 回答