Ios nsmutablearray addobject添加object.text两次
使用addObject会将对象添加两次Ios nsmutablearray addobject添加object.text两次,ios,objective-c,nsmutablearray,Ios,Objective C,Nsmutablearray,使用addObject会将对象添加两次 AppDelegate *appdelegate = [[UIApplication sharedApplication] delegate]; NSLog(@"LOG1 %@", object.text); [appdelegate.mutarray addObject:object.text]; NSLog(@"LOG2 %@", appdelegate.mutarray); Log1返回:Log1值
AppDelegate *appdelegate = [[UIApplication sharedApplication] delegate];
NSLog(@"LOG1 %@", object.text);
[appdelegate.mutarray addObject:object.text];
NSLog(@"LOG2 %@", appdelegate.mutarray);
Log1返回:Log1值
Log2返回:Log2(
“价值”,
“价值”
)
为什么要加两次呢?removeObject同时删除两个,我可以只删除一个吗?因为您第一次通过向NSMutableArray添加值时,没有从NSMutableArray中删除值。到目前为止,它可能会增加并发重复值。在添加新值之前,请删除整个数组
if(appdelegate.mutarray.count!=0)
[appdelegate.mutarray removeAllObject];
[appdelegate.mutarray addObject:object.text];
在添加对象之前记录数组。需要查看获取对象的代码和删除对象的代码。removeAllObjects可以工作[不重复,只输出一个值],但我不想全部删除,因为我想保留其他收藏夹。。我尝试了[appdelegate.mutarray removeObject:object.text];但只允许删除一个基于favoriteuse索引的对象,比如RemoveObjectAtIndex,所以很明显我之前也在某个地方添加了它。谢谢你