Search code examples
objective-carrayscocoansarraynsarraycontroller

I can't remove an object from a NSMutableArray


I don't understand why an object is not removed from an NSMutableArray. This is the code:

[self willChangeValueForKey:@"candidatesProxy"];
[candidatesProxy removeObject:[[pseudonymsArrayController selectedObjects] lastObject]];
[self didChangeValueForKey:@"candidatesProxy"];

I've checked and the lastObjet in pseudonyms is the same object of candidatesProxy. But it is not removed.

Is it because maybe the object has been copied to a different memory location, so I actually have 2 objects rather then one ?

Thanks


Solution

  • You cannot remove objects from NSArray, you can remove it only from NSMutableArray

    EDIT

    Just try the following statements before willChangeValueForKey:

    NSLog(@"Array : %@", candidatesProxy);
    NSLog(@"Element : %@",[[pseudonymsArrayController selectedObjects] lastObject]);
    

    and check whether the lastObject element is present in the array.