0

I have property:

@property NSMutableArray *fieldsArray;
@synthesize fieldsArray;

and in method I try to get something from this array:

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

But then I get error :

NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 2147483647 beyond bounds [0 .. 35]'

So array is not accesible.

How to do it visible?

mmmmmm
  • 32,227
  • 27
  • 88
  • 117
netmajor
  • 6,507
  • 14
  • 68
  • 100

2 Answers2

1

The array is accessible, it's just that the index that you are trying to pass is beyond bounds, probably because the number is negative.

The index 2147483647 looks like a negative 1 re-interpreted as a positive number. Make sure that firstBallIndex is between 0 and 35 before making a call to the objectAtIndex: method.

Since you use the indexOfObject: method to look for objects in NSArray, make sure that your objects properly implement the isEqual: method. Otherwise, NSArray will not be able to find your custom objects, unless you pass the exact instance to it.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • `2147483647` is the value of the built-in `NSNotFound` constant, that's returned when `indexOfObject:` is unable to find the specified object. – cutsoy May 05 '13 at 13:29
  • @TimvanElsloo Right, which happens to have [the same bit pattern as -1](http://stackoverflow.com/q/9338295/335858). – Sergey Kalinichenko May 05 '13 at 13:35
1

This has nothing to do with the array itself, but instead with the value of self.firstBall. My best guess is that it's nil, which would make firstBallIndex NSNotFound (2147483647). Obviously, 2147483647 is beyond the bounds of your array, which then raises this error.

Depending on your implementation, you could do something like this:

if (firstBallIndex != NSNotFound)
    // which means that self.firstBall wasn't in your array in the first palce.

or

if ([self.fieldsArray containsObject:self.firstBall])
    // which basically does the same, but in a more stylish way.
cutsoy
  • 10,127
  • 4
  • 40
  • 57
  • Yes, You right - self.firstBall in nil when go to animationDidStop, I dont know why, because I don't reset this property, and before start animationDidStop method this property is set properly :/ – netmajor May 05 '13 at 13:26