gpt4 book ai didi

iphone - NSMutableArray 检查对象是否已经存在

转载 作者:IT老高 更新时间:2023-10-28 11:40:51 25 4
gpt4 key购买 nike

我不知道该怎么做。我有一个 NSMutableArray (addList),其中包含要添加到我的数据源 NSMutableArray 的所有项目。

我现在想检查要从 addList 数组添加的对象是否已存在于数据源数组中。如果不存在则添加该项目,如果存在则忽略。

这两个对象都有一个我想比较的名为 iName 的字符串变量。

这是我的代码片段

-(void)doneClicked{
for (Item *item in addList){
/*
Here i want to loop through the datasource array
*/
for(Item *existingItem in appDelegate.list){
if([existingItem.iName isEqualToString:item.iName]){
// Do not add
}
else{
[appDelegate insertItem:item];
}
}
}

但我发现要添加的项目即使存在。

我做错了什么?

最佳答案

在 NSArray 中有一个非常有用的方法,即 containsObject

NSArray *array;
array = [NSArray arrayWithObjects: @"Nicola", @"Margherita", @"Luciano", @"Silvia", nil];
if ([array containsObject: @"Nicola"]) // YES
{
// Do something
}

关于iphone - NSMutableArray 检查对象是否已经存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6291412/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com