我有一个属性:
@property NSMutableArray *fieldsArray;
@synthesize fieldsArray;
在方法中,我尝试从这个数组中获取一些东西:
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag{
NSInteger firstBallIndex = [self.fieldsArray indexOfObject:self.firstBall];
}
但是我得到错误:
NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]:索引2147483647越界[0 ..]35]
数组不可访问
如何使它可见?
这与数组本身无关,而是与self.firstBall
的值有关。我最好的猜测是nil
,这将使firstBallIndex
成为NSNotFound
(2147483647
)。显然,2147483647
超出了数组的边界,这会引发此错误。
根据你的实现,你可以这样做:
if (firstBallIndex != NSNotFound)
// which means that self.firstBall wasn't in your array in the first palce.
或
if ([self.fieldsArray containsObject:self.firstBall])
// which basically does the same, but in a more stylish way.
数组是可访问的,只是你试图传递的索引超出了界限,可能是因为数字是负的。
索引2147483647
看起来像一个负1重新解释为一个正数。在调用objectAtIndex:
方法之前,请确保firstBallIndex
在0到35之间。
由于您使用indexOfObject:
方法来查找NSArray
中的对象,请确保您的对象正确地实现了isEqual:
方法。否则,NSArray
将无法找到您的定制对象,除非您将确切的实例传递给它。