I don't understand why an object is not removed from an NSMutableArray. This is the code:
我不明白为什么没有从NSMutableArray中删除对象。这是代码:
[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.
我已经检查过,假名中的lastObjet与candidateProxy的对象相同。但它没有被删除。
Is it because maybe the object has been copied to a different memory location, so I actually have 2 objects rather then one ?
是因为可能该对象已被复制到不同的内存位置,所以我实际上有2个对象而不是一个?
Thanks
2 个解决方案
#1
1
You cannot remove objects from NSArray
, you can remove it only from NSMutableArray
您无法从NSArray中删除对象,只能从NSMutableArray中删除它
EDIT
Just try the following statements before willChangeValueForKey:
在willChangeValueForKey之前尝试以下语句:
NSLog(@"Array : %@", candidatesProxy);
NSLog(@"Element : %@",[[pseudonymsArrayController selectedObjects] lastObject]);
and check whether the lastObject element is present in the array.
并检查数组中是否存在lastObject元素。
#2
3
When you use the removeObject:
method on, the index of the object is first determined by sending indexOfObject:
to that array, and then the object at that index is removed.
当您使用removeObject:方法时,首先通过将indexOfObject:发送到该数组来确定对象的索引,然后删除该索引处的对象。
In order to make this working, ensure your custom object class override -(BOOL)isEqual:
.
为了使其工作,请确保您的自定义对象类覆盖 - (BOOL)isEqual:。
Alternatively, remove the object using an index, but you first need to determine it.
或者,使用索引删除对象,但首先需要确定它。
#1
1
You cannot remove objects from NSArray
, you can remove it only from NSMutableArray
您无法从NSArray中删除对象,只能从NSMutableArray中删除它
EDIT
Just try the following statements before willChangeValueForKey:
在willChangeValueForKey之前尝试以下语句:
NSLog(@"Array : %@", candidatesProxy);
NSLog(@"Element : %@",[[pseudonymsArrayController selectedObjects] lastObject]);
and check whether the lastObject element is present in the array.
并检查数组中是否存在lastObject元素。
#2
3
When you use the removeObject:
method on, the index of the object is first determined by sending indexOfObject:
to that array, and then the object at that index is removed.
当您使用removeObject:方法时,首先通过将indexOfObject:发送到该数组来确定对象的索引,然后删除该索引处的对象。
In order to make this working, ensure your custom object class override -(BOOL)isEqual:
.
为了使其工作,请确保您的自定义对象类覆盖 - (BOOL)isEqual:。
Alternatively, remove the object using an index, but you first need to determine it.
或者,使用索引删除对象,但首先需要确定它。