I have an NSMutableArray which I assign with:
NSMutableArray *newElements = [[NSMutableArray alloc] initWithObjects:self.currentScene.elements, nil];
//selectedElement is assigned somewhere above this, shouldn't be relevant since it's the same object as the one in the array
int indexToRemove = [self.currentScene.elements indexOfObject:selectedElement];
I’m removing selectedElement from this array, but seeing some bizarre behaviour in the debugger. After initializing the array, by setting a breakpoint before removing selectedElement, I see this:
po newElements
(NSMutableArray *) $2 = 0x08c74e90 <__NSArrayM 0x8c74e90>(
<__NSArrayM 0x8c52e60>(
<StoryTextElement: 0x12ac6e00>,
<StoryTextElement: 0x8ca1a50>
)
)
(lldb) po selectedElement
(StoryElement *) $3 = 0x08ca1a50 <StoryTextElement: 0x8ca1a50>
I;m trying to remove the object with with:
NSLog(@"count: %d", [newElements count]); // prints count: 1
[newElements removeObject:selectedElement];
NSLog(@"count: %d", [newElements count]); // prints count: 1
[newElements removeObjectAtIndex:indexToRemove]; // throws an exception. indexToRemove is 1 in the debugger
NSLog(@"count: %d", [newElements count]);
I don’t understand why my object isn’t removed. It’s like I’m missing the point.
You have an array that contains one object, which is another array.
newElementsonly has one valid index, and that is0. You need to change your first line to look like