NSMutableArray检查对象是否已存在 [英] NSMutableArray check if object already exists

查看:83
本文介绍了NSMutableArray检查对象是否已存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道该如何解决这个问题。我有一个 NSMutableArray (addList),它包含要添加到我的数据源NSMutableArray的所有项目。

I am not sure how to go about this. I have an NSMutableArray (addList) which holds all the items to be added to my datasource NSMutableArray.

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

I now want to check if the object to be added from the addList array already exists in the datasource array. If it does not exist add the item, if exists ignore.

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

Both the objects have a string variable called iName which i want to compare.

这是我的代码片段

-(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];
            } 
        }
}

但我找到的项目是即使它存在也要添加。

But i find the item to be added even if it exists.

我做错了什么?

推荐答案

我找到了解决方案,可能不是最有效的,但至少是作品

I found a solution, may not be the most efficient of all, but atleast works

NSMutableArray *add=[[NSMutableArray alloc]init];

for (Item *item in addList){
        if ([appDelegate.list containsObject:item])
            {}
        else
            [add addObject:item];
}

然后我迭代添加数组并插入项目。

Then I iterate over the add array and insert items.

这篇关于NSMutableArray检查对象是否已存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆