Eliminar objeto de CCArray no funciona

I'd like to remove a NSNumber from a CCArray. When I use removeObject: to remove a NSNumber of 13 or above nothing gets removed. What's going on here? I've tried using an NSMutableArray and it works fine. Using removeObjectAtIndex: is not an option because my array wont be in numerical order.

CCArray *arr = [CCArray array];

for (NSInteger i = 0; i < 20; i++) {
    [arr addObject:[NSNumber numberWithInt:i]];

// Outputs arr count 20 
CCLOG(@"%@: arr count %i ", NSStringFromSelector(_cmd), [arr count]);

[arr removeObject:[NSNumber numberWithInt:13]];

// Still outputs arr count 20  
CCLOG(@"%@: arr count %i ", NSStringFromSelector(_cmd), [arr count]);

preguntado el 09 de marzo de 12 a las 16:03

What does [arr getIndexOfObject:[NSNumber numberWithInt:13]] return? A valid index, or NSNotFound? It might be worth printing out what each element in arr is, just as a sanity check. I can't see why it wouldn't be there... -

CCLOG(@" %i ", [arr indexOfObject:[NSNumber numberWithInt:13]]); placed at the end of the above code returns 2147483647. -

I'm guessing that's NSNotFound. I think it is something to do with equality testing of objects. Create two separate variables of type NSNumber, both set to 13, then test one against the other - does it see them as equal? (Sorry, I don't know coco). -

1 Respuestas

I can verify that this seems to be a problem. Although I was able to call removeObject successfully, indexOfObject: returned NSNotFound. I rewrote the test case, and the error was gone. One more reason to stay away from CCArray I suppose.

I suggest to use NSMutableArray in this case. The slightly better performance of CCArray will not be reflected in better framerate, especially not for an array that has 20 elements. Besides, correct code is always better than code that's faster but incorrect.

respondido 09 mar '12, 20:03

It's kind of ironic that your screen name is 'Learn Cocos 2D', and yet you are saying to not use a fundamental part of cocos2d. - Ricardo J. Ross III

So I took the time to help you diagnose that problem... and he gets the credit? Thanks a bunch. - JTeagle

@Richard: hence my screen name isn't 'Love Cocos2D' :) There's plenty of things about Cocos2D that I would have done differently. Not replacing NSMutableArray with CCArray is what I would have done, especially after performance tests showed that you can get only around 5% better framerate in synthetic tests. CCArray has proven to be problematic in a number of ways. I'm not using it anymore in my own code, because if I ever need more performance, there's always plenty of other (and better) opportunities for optimization. - AprendeCocos2D

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.