2013-05-05 67 views
0

我有属性:用不上在animationDidStop法合成财产

@property NSMutableArray *fieldsArray; 
@synthesize fieldsArray; 

和方法,我试图从这个数组的东西:

- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag{ 
    NSInteger firstBallIndex = [self.fieldsArray indexOfObject:self.firstBall]; 
} 

但后来我得到错误:

NSRangeException '原因:' * - [__ NSArrayM objectAtIndex:]:索引 2147483647超出范围[0 .. 35]'

因此数组不可访问。

如何做到这一点可见?

回答

1

这与阵列本身无关,而是与self.firstBall的值相反。我最好的猜测是它是nil,这将使firstBallIndexNSNotFound2147483647)。显然,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. 
+0

是的,你正确 - self.firstBall在无到animationDidStop时,我不知道为什么,因为我没有重置此属性,并且在开始animationDidStop方法之前,此属性设置正确:/ – netmajor 2013-05-05 13:26:34

+0

'NSLog(...) '和断点是你最好的朋友:) – elslooo 2013-05-05 13:30:13

1

该数组是可访问的,它只是你试图通过的索引超出了界限,可能是因为数字是负数。

索引2147483647看起来像一个负数1重新解释为一个正数。在拨打objectAtIndex:方法之前,请确保firstBallIndex介于0和35之间。

由于您使用indexOfObject:方法查找NSArray中的对象,因此请确保您的对象正确实施了isEqual:方法。否则,NSArray将无法​​找到您的自定义对象,除非您将确切的实例传递给它。

+0

'2147483647'是内置'NSNotFound'不变,这是返回的值时' indexOfObject:'无法找到指定的对象。 – elslooo 2013-05-05 13:29:37

+0

@TimvanElsloo正确的,恰好有[与-1相同的位模式](http://stackoverflow.com/q/9338295/335858)。 – dasblinkenlight 2013-05-05 13:35:26