Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios [\uu NSArrayM objectAtIndex:]:索引517431040超出范围[0..48]_Ios_Objective C_Arrays - Fatal编程技术网

Ios [\uu NSArrayM objectAtIndex:]:索引517431040超出范围[0..48]

Ios [\uu NSArrayM objectAtIndex:]:索引517431040超出范围[0..48],ios,objective-c,arrays,Ios,Objective C,Arrays,我喜欢这个错误,但得出的结论是,发生错误是因为数组为空。然而,我的数组不是空的 -(void)setNumberInTippfield:(NSNumber *)aNumber{ NSNumber *index = [self.ticket.currentSlip intValue]-1; NSLog(@"index: %i",index);//prints 0, expected Slip *slip = [self.slipArray objectAtIndex:index];

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

-(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告诉我正确的值。关于这个有什么提示吗?非常感谢。

对象索引:
采用
int
,而不是
NSNumber

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

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

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

结果不应该是
int
?这将解决接下来的问题。

哪个调用
objectAtIndex
引发此异常?最后一行,请参阅我的编辑。因为NSLog打印的是[object description]的值,而不是对象本身。stackoverflow上有很多“如何执行数据,显示代码”的问题,我得到了否决票……第二个
objectAtIndex:
super实例也是如此!很有效,谢谢!但是,它的第一个实例是有效的,奇怪。@dan-它不是“奇怪的”,您已将
int
值(
[self.ticket.currentSlip intValue]-1
0分配给指针-这在C中是合法的。然后,您将该指针作为整数传递
[self.slipArray objectAtIndex:index]
,同样合法,而且一切正常。您需要确保了解基本值类型(如
int
)与表示值的对象(如
NSNumber
)之间的区别——两者不可互换。(可能您习惯于C#其中(un)装箱是自动的?)我明白,虽然我缺乏这方面的深入知识,或者不想太多。非常感谢你提供的信息。