我想我会为简单的事情生气。
如下代码:
NSLog(@"potentialPositions: %@, with count: %i", potentialPositions, [potentialPositions count]);
for (NSUInteger i = 1; i < (choices); i++) {
NSLog(@"i = %i",i);
[self drawAWord:[[currentLevel objectAtIndex:levelLink] objectAtIndex:i] atPoint:[[potentialPositions objectAtIndex:i] CGPointValue] withInitialZoom:FALSE];
}
产生以下结果:
2014-02-15 20:31:54.329 ThesaurusLinks[12446:70b] potentialPositions: (
"NSPoint: {180, 125}",
"NSPoint: {270, 200}",
"NSPoint: {350, 250}",
"NSPoint: {280, 160}"
), with count: 4
2014-02-15 20:31:54.330 ThesaurusLinks[12446:70b] i = 1
2014-02-15 20:31:54.331 ThesaurusLinks[12446:70b] i = 2
2014-02-15 20:31:54.331 ThesaurusLinks[12446:70b] i = 3
2014-02-15 20:31:54.334 ThesaurusLinks[12446:70b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 3 beyond bounds [0 .. 2]'
数组中显然有四个对象称为“potentialPositions”。为什么它认为边界只有0..2?当我放置一个NSLOG时,它不能出现在我的DrawAWord方法中(结果未在上面显示),对于i = 3不会被调用。我很困惑。
感谢您的任何答案或提示。
最佳答案
什么
[currentLevel objectAtIndex:levelLink]
退货?似乎问题出在这里。加
NSLog(@"%@",[currentLevel objectAtIndex:levelLink]);
看看它有多少元素。
关于ios - NSArray索引超出范围,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21806226/