2

I'm pretty new to iOS, and I have no idea how to find a decent stacktrace like JAVA, so all I can find on why it's crashing is, "sigabrt".

I know it has something to do with this code I just added.

-(void) clearGame {
    for (CCSprite *sprite in placedSprites) {
        if(sprite == nil) continue;
        [self removeChild:sprite cleanup:NO];
        [placedSprites removeObject:sprite];
    }
    placedSprites = [[NSMutableArray alloc] initWithCapacity:1000];
}

Where the class I'm adding this to is a Layer in cocos2d. In it's init method, I have

placedSprites = [[NSMutableArray alloc] initWithCapacity:1000];

I don't know what could be wrong, so any help is appreciated.

Thank you!

Austin
  • 4,801
  • 6
  • 34
  • 54
  • When you see just "SIGABRT" and no stack trace nor the details of the exception, add an "Exception breakpoint" in XCode. Details at http://stackoverflow.com/questions/4961770/run-stop-on-objective-c-exception-in-xcode-4 – kuba Feb 28 '12 at 21:40

1 Answers1

7

You cannot remove an object from a NSMutableArray while fast-enumerating (see documentation).

You could add the objects (which should be removed) to a separate NSMutableArray and remove the objects from this array from your 'main' array:

NSMutableArray *discardedItems = [NSMutableArray array];
SomeObjectClass *item;

for (item in originalArrayOfItems) {
    if ([item shouldBeDiscarded])
        [discardedItems addObject:item];
}

[originalArrayOfItems removeObjectsInArray:discardedItems];

Also see Removing object from NSMutableArray and Best way to remove from NSMutableArray while iterating?.

Community
  • 1
  • 1
tilo
  • 14,009
  • 6
  • 68
  • 85