我有一个 NSMutableArray,我分配给它:
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];
我正在selectedElement
从这个数组中删除,但在调试器中看到了一些奇怪的行为。初始化数组后,通过在删除之前设置断点selectedElement
,我看到了:
po newElements
(NSMutableArray *) $2 = 0x08c74e90 <__NSArrayM 0x8c74e90>(
<__NSArrayM 0x8c52e60>(
<StoryTextElement: 0x12ac6e00>,
<StoryTextElement: 0x8ca1a50>
)
)
(lldb) po selectedElement
(StoryElement *) $3 = 0x08ca1a50 <StoryTextElement: 0x8ca1a50>
我;正在尝试使用以下方法删除对象:
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]);
我不明白为什么我的对象没有被删除。这就像我错过了重点。