2013-05-05 65 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