我最近开始使用 cocoa 和 Objective-C,虽然我对软件概念并不陌生,而且我在 NSMutableArray 类中遇到了一些奇怪且出乎意料的行为。
首先,我创建并填充了一个“NSMutableArray”对象,其中我的自定义对象“CustomClass”类工作正常。然后我继续尝试删除其中一个对象,但我没有得到我想要的结果。
// NSMutableArray *Array is already initialized and loaded with 5 CustomClass Objects
// stored in variables 'CustomClassObject1' through 'CustomClassObject5'.
[Array removeObject:CustomClassObject4];
这段代码并没有像我想要的那样简单地删除第四个条目,而是将第四个条目设置为“nil”(即 (id) 0x00000000),并且更令人困惑的是,删除了数组中的最后一个条目。
在我的理解中,这不应该发生——应该发生的只是第四个条目应该被删除,由第五个条目代替。相反,我现在有一个包含三个对象和一个 nil 的数组!
我也试过
int position = [Array indexOfObject:CustomClassObject4];
[Array removeObjectatIndex:position];
只为得到同样的结果。有谁知道为什么会这样?我是不是误会了什么?
谢谢!
代码:
NSMutableArray *Array = [NSMutableArray array];
CustomClass *CustomClassObject1 = [[CustomClass alloc] init];
CustomClass *CustomClassObject2 = [[CustomClass alloc] init];
CustomClass *CustomClassObject3 = [[CustomClass alloc] init];
CustomClass *CustomClassObject4 = [[CustomClass alloc] init];
CustomClass *CustomClassObject5 = [[CustomClass alloc] init];
[Array AddObject:CustomClassObject1];
[Array AddObject:CustomClassObject2];
[Array AddObject:CustomClassObject3];
[Array AddObject:CustomClassObject4];
[Array AddObject:CustomClassObject5];
此时输出:
_Array = (NSMutableArray *) 0x0885c2c0 @ "5 Objects"
[0] = (CustomClass *) 0x0885c5f0
[1] = (CustomClass *) 0x0885c630
[2] = (CustomClass *) 0x0885c690
[3] = (CustomClass *) 0x0885c6f0
[4] = (CustomClass *) 0x0885c730
然后:
[Array removeObject:CustomClassObject4];
此时输出:
_Array = (NSMutableArray *) 0x0885c2c0 @ "4 Objects"
[0] = (CustomClass *) 0x0885c5f0
[1] = (CustomClass *) 0x0885c630
[2] = (CustomClass *) 0x0885c690
[3] = (id) 0x00000000
相反,如果我删除第三个对象,
[Array removeObject:CustomClassObject3];
输出是:
_Array = (NSMutableArray *) 0x0885c2c0 @ "4 Objects"
[0] = (CustomClass *) 0x0885c5f0
[1] = (CustomClass *) 0x0885c630
[2] = (id) 0x00000000
[3] = (CustomClass *) 0x0885c6f0