NSMutable Array removeObjectAtIndex正在删除两个对象



我有一个NSMutableArray,有5个对象

[0] StateGeographicComponent *  0x0a455ba0
[1] StateGeographicComponent *  0x11c374b0
[2] StateGeographicComponent *  0x08e5f720
[3] StateGeographicComponent *  0x11c15430
[4] StateGeographicComponent *  0x0a458f20

执行以下代码行后:

[self.gameComponents removeObjectAtIndex:0];

而不是像我所期望的那样简单地删除索引0处的对象并将所有内容向上滑动到索引中。索引[0]处的对象设置为nil,索引[4]处的对象被删除。

[0] id  0x00000000
[1] StateGeographicComponent *  0x11c374b0
[2] StateGeographicComponent *  0x08e5f720
[3] StateGeographicComponent *  0x11c15430

我很困惑。

使用以下代码行

[self.gameComponents replaceObjectAtIndex:0 withObject:[NSNull null]];
[self.gameComponents removeLastObject];

第一行代码用NSNull的singleton实例替换对象,第二行代码从数组中删除最后一个对象。

最新更新