Question

I get NSRangeException error when I try to removeObjectAt:0 . I don't know how that is even possible.

-(void)removePoints:(ccTime *)tm
{

    NSLog(@"naughty %@",_naughtytoucharray);
    if ([_naughtytoucharray count]>0)
    {
        [_naughtytoucharray removeObjectAtIndex:0];
    }
}

shows on log:

[4195:c07] naughty ( "{124, 98}", "{123, 98}", "{135, 97}", "{124, 98}", "{148, 94}", "{135, 97}", "{157, 93}", "{148, 94}", "{162, 92}", "{157, 93}", "{164, 92}", "{162, 92}" )

then I get the following error: *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 11 beyond bounds [0 .. 10]'

When I set a breakpoint I see all objects are 0x0

enter image description here

NSMutableArray is allocated and initialized in init and filled in ccTouchesMoved

-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event  {


    if (isSelected) {

        UITouch *touch = [ touches anyObject];
        CGPoint new_location = [touch locationInView: [touch view]];
        new_location = [[CCDirector sharedDirector] convertToGL:new_location];


        CGPoint oldTouchLocation = [touch previousLocationInView:touch.view];
        oldTouchLocation = [[CCDirector sharedDirector] convertToGL:oldTouchLocation];
        oldTouchLocation = [self convertToNodeSpace:oldTouchLocation];

        // add my touches to the naughty touch array
        [_naughtytoucharray addObject:NSStringFromCGPoint(new_location)];
        [_naughtytoucharray addObject:NSStringFromCGPoint(oldTouchLocation)];

    }
}

Any idea why I cannot remove the first item of the array?

Was it helpful?

Solution

That's not where your error is coming from. The method it's complaining about is objectAtIndex:, not removeObjectAtIndex: and the index causing the error is eleven, not zero.

Look for a place in your code where you are assuming that there are at least 12 objects in the array while reading from it. There are only eleven and their indexes are 0-to-10.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top