我无法从 NSMutableArray 中删除对象
我不明白为什么不从 NSMutableArray 中删除对象。这是代码:
[self willChangeValueForKey:@"candidatesProxy"];
[candidatesProxy removeObject:[[pseudonymsArrayController selectedObjects] lastObject]];
[self didChangeValueForKey:@"candidatesProxy"];
我检查过,假名中的lastObjet与candidatesProxy的对象相同。但它并没有被删除。
是否因为该对象可能已被复制到不同的内存位置,所以我实际上有 2 个对象而不是一个?
谢谢
I don't understand why an object is not removed from an NSMutableArray. This is the code:
[self willChangeValueForKey:@"candidatesProxy"];
[candidatesProxy removeObject:[[pseudonymsArrayController selectedObjects] lastObject]];
[self didChangeValueForKey:@"candidatesProxy"];
I've checked and the lastObjet in pseudonyms is the same object of candidatesProxy. But it is not removed.
Is it because maybe the object has been copied to a different memory location, so I actually have 2 objects rather then one ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您使用
removeObject:
方法时,首先通过向该数组发送indexOfObject:
来确定对象的索引,然后删除该索引处的对象。为了使其正常工作,请确保您的自定义对象类覆盖
-(BOOL)isEqual:
。或者,使用索引删除对象,但您首先需要确定它。
When you use the
removeObject:
method on, the index of the object is first determined by sendingindexOfObject:
to that array, and then the object at that index is removed.In order to make this working, ensure your custom object class override
-(BOOL)isEqual:
.Alternatively, remove the object using an index, but you first need to determine it.
您无法从
NSArray
中删除对象,只能从NSMutableArray
中删除它编辑
只需在
willChangeValueForKey 之前尝试以下语句:
并检查数组中是否存在lastObject 元素。
You cannot remove objects from
NSArray
, you can remove it only fromNSMutableArray
EDIT
Just try the following statements before
willChangeValueForKey:
and check whether the lastObject element is present in the array.