有一个数组,内部元素是字典,
NSArray *array = @[@{@"name":@"李大嘴",
@"age":@"18"},
@{@"name":@"白展堂",
@"age":@"20"},
@{@"name":@"吕秀才",
@"age":@"26"},
@{@"name":@"李大嘴",
@"age":@"18"},];
_mutableArray = [NSMutableArray array];
[_mutableArray addObjectsFromArray:array];然后取出第一个字典(李大嘴),在数组中移除
NSDictionary *dic = _mutableArray.firstObject;
[_mutableArray removeObject:dic];
NSLog(@"%@",_mutableArray);我以为本该移除的只有第一个,却发现最后一个李大嘴也被移除了
<__NSArrayM 0x600002e0c210>(
{
age = 20;
name = "白展堂";
},
{
age = 26;
name = "吕秀才";
}
)简单点描述就是,如果一个数组中的元素是字典、通过removeObjec方法移除一个字典,那么相同数据的字典也会被移除,有没有大佬给解答一下为什么?!在线等。
2019.11.14 更新
苹果官方文档给的解释是:Removes all occurrences in the array of a given object.(删除给定对象数组中出现的所有内容。)
注意是所有内容。
This method determines a match by comparing anObject to the objects in the receiver using the isEqual: method. If the array does not contain anObject, the method has no effect (although it does incur the overhead of searching the contents).
上面内容的意思是:此方法通过使用isEqual:方法将对象与接收方中的对象进行比较来确定是否匹配。如果匹配,删除。
因为 第一个字典isEqual:最后一个字典 条件成立,所以最后一个字典也会被删除。