Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c NSMutableAray RemoveObject:未按预期工作_Objective C_Nsmutablearray - Fatal编程技术网

Objective c NSMutableAray RemoveObject:未按预期工作

Objective c NSMutableAray RemoveObject:未按预期工作,objective-c,nsmutablearray,Objective C,Nsmutablearray,我最近开始使用cocoa和Objective-C,尽管我对软件概念并不陌生,而且我在NSMutableArray类中遇到了一些奇怪和意外的行为 首先,我使用“CustomClass”类的自定义对象创建并填充一个“NSMutableArray”对象,该类工作正常。然后我继续尝试删除其中一个对象,但没有得到想要的结果 // NSMutableArray *Array is already initialized and loaded with 5 CustomClass Objects // s

我最近开始使用cocoa和Objective-C,尽管我对软件概念并不陌生,而且我在NSMutableArray类中遇到了一些奇怪和意外的行为

首先,我使用“CustomClass”类的自定义对象创建并填充一个“NSMutableArray”对象,该类工作正常。然后我继续尝试删除其中一个对象,但没有得到想要的结果

// NSMutableArray *Array is already initialized and loaded with 5 CustomClass Objects
//   stored in variables 'CustomClassObject1' through 'CustomClassObject5'. 
[Array removeObject:CustomClassObject4];
这段代码没有像我希望的那样简单地删除第四个条目,而是将第四个条目设置为“nil”(即,(id)0x00000000),更令人困惑的是,删除了数组中的最后一个条目

在我的理解中,这是不应该发生的-所有应该发生的是第四个条目应该被删除,取而代之的是第五个条目。相反,我现在有一个包含三个对象和一个零的数组

我也试过了

int position = [Array indexOfObject:CustomClassObject4];
[Array removeObjectatIndex:position];
只是为了得到同样的结果。有人知道为什么会这样吗?我是不是误解了什么

谢谢

代码:

此时的输出:

_Array = (NSMutableArray *) 0x0885c2c0 @ "5 Objects"
[0] = (CustomClass *) 0x0885c5f0
[1] = (CustomClass *) 0x0885c630
[2] = (CustomClass *) 0x0885c690
[3] = (CustomClass *) 0x0885c6f0
[4] = (CustomClass *) 0x0885c730
_Array = (NSMutableArray *) 0x0885c2c0 @ "4 Objects"
[0] = (CustomClass *) 0x0885c5f0
[1] = (CustomClass *) 0x0885c630
[2] = (CustomClass *) 0x0885c690
[3] = (id) 0x00000000
然后:

此时的输出:

_Array = (NSMutableArray *) 0x0885c2c0 @ "5 Objects"
[0] = (CustomClass *) 0x0885c5f0
[1] = (CustomClass *) 0x0885c630
[2] = (CustomClass *) 0x0885c690
[3] = (CustomClass *) 0x0885c6f0
[4] = (CustomClass *) 0x0885c730
_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

你能试试简单一点的吗

NSMutableArray *array = [NSMutableArray new];
[array addObject: [[CustomClass alloc] init] ];
// repeat for as many times you want.

// Fast enumerate the loop and get an output - You class will need to provide method id
for ( CustomClass * c in array ) NSLog( @"Class id: %@", [c id] );

// Then try to delete any random object.
[array removeObjectAtIndex: [array indexOfObject: object] ];

// And repeat the enumeration
for ( CustomClass * c in array ) NSLog( @"Class id: %@", [c id] );
你能试试这个,告诉我们发生了什么事吗? 另外,在每个添加和删除操作中添加断点


确保已启用arc,并且代码在自动释放池中

这一切归结为ARC如何处理局部变量。现在,我将假装关闭ARC,向您展示代码是如何进行内存管理的,以便于演示:

NSMutableArray *Array = [NSMutableArray array];

//All of these are initialized with a +1 retain count
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];

//The array retains what gets put into it, meaning it owns these variables
//And gives them a total reference count of +2
[Array AddObject:[CustomClassObject1 retain]];
[Array AddObject:[CustomClassObject2 retain]];
[Array AddObject:[CustomClassObject3 retain]];
[Array AddObject:[CustomClassObject4 retain]];
[Array AddObject:[CustomClassObject5 retain]];

//The compiler inserts a release for each of those objects because you
//Don't reference them before the method's scope ends.  The array still owns
//them, so they all have a total reference count of +1 at this point.
[CustomClassObject1 release];
[CustomClassObject2 release];
[CustomClassObject3 release];
[CustomClassObject4 release];
[CustomClassObject5 release];

删除对象时,数组会减少其引用计数,因此当删除
CustomClassObject4
时,它的引用计数会下降到一个很大的0,并被释放。这就是你看到的零。要修复它,请在强IVAR、属性、,或全局。

将对象设置为数组后,打印数组的所有值并检查最后一行是否存在。可能是最后一个类未初始化请尝试在此处发布整个代码,以便我们了解您如何初始化它以及插入对象后数组中的内容。在设置数组中的对象后,通过打印数组显示输出。是否确定
NSMutableArray
是否表现出这种行为?发布用于获取这些意外结果的代码。NSArray无法存储nil。如何确定数组的第四个元素是nil?我假设它是nil对象。在这一点上我可能弄错了。代码posted调用removeObjectAtIndex后,该代码按预期工作。谢谢,我可以以此为出发点。真的,我只是想知道以前是否有人看到过,谢谢大家的帮助。内存管理是另一个问题,它导致了多次错误,因为调用已发布的对象已经退出应用程序。+1对于buddyThanks CodaFi,这听起来很有意义,但是,在删除CustomClassObject4之前,我将其添加到另一个数组中,同样的结果也发生了。然后,该数组超出范围并被释放。我将该对象添加到从第一个数组中删除它之前的第二个数组中。这还超出范围吗?如果是这样的话,我可以在同一个作用域中创建一个强变量并将其存储在那里吗?不可以。您需要将它存储在某个不会在方法的堆栈帧被压碎时消失的地方。把它储存在iVar里。