如何避免NSMutableArray中的重复字典对象 [英] How to avoid the duplicate dictionary objects in NSMutableArray

查看:157
本文介绍了如何避免NSMutableArray中的重复字典对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个应用程序中,您好,我有一个包含一组NSMutableDictionary对象的数组.字典对象具有三个键值对,如下所示

Hi in one of my application,I have an array which contains a group of NSMutableDictionary objects. The dictionary object have three key-value pairs as like below

  1. 公司
  2. 产品
  3. 数量

具有许多对象的数组.现在,通过使用不同的添加按钮,我将这些字典对象添加到数组中.即使在将对象添加到数组时,我也在使用NSNotFound方法检查是否有重复的对象可用.就像这样

And array having many number of objects. Here now by using different add buttons I am adding these dictionary objects to the array. Even while adding objects to array i am checking whether any duplicate objects are available or not using NSNotFound method. As such below

if([Array indexOfObject:dicObject] == NSNotFound)
{  
        [Array addObject:dicObject];
}

在某些情况下它可以正常工作,但在其他情况下却无法工作.我将举一个例子进行说明:

Here it is working fine in few cases, But it's not working in other cases.I will explain with one example :

  1. 例如,我在数组中有一个dicobject,后面是键值对

  1. For example i have one dicobject in array with following key value pairs

company:XYZ Product:ABC Quantity:2

例如,现在我想添加一个以上相同的键值对的dic对象.显然,那一次它不会添加,因为阵列中已经有相同的产品.

Now for example I want to add one more dic object with the same above key value pairs. That time obviously it won't add because already same product is available in array.

这是有效条件.

例外情况:例如,我想再添加一个具有以下值的产品

Exceptional Case: For example I want to add one more product with following values

Company:XYZ    Product:ABC   Quantity:6

在这种情况下,此产品没有任何错误地添加到阵列中.但我担心的是,我不想将其再次添加到阵列中,而只需要更新数量,因为公司和产品名称都相同.所以,请您告诉我解决这种情况的方法.

At this case this product is adding into the array without any error. But my concern is i don't want to add this into the array again only the quantity have to update, because company and product name both are same so. So can you please show me the way to handle this scenario.

推荐答案

您可以使用

You could use indexOfObjectPassingTest: to know if a similar dictionary is already present in the array.

这可能看起来像这样:

NSMutableArray *arr = // your array
NSDictionary *dicObject = // your object

NSUInteger indexOfDicObject = [arr indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop)
{
    return ([obj[@"company"] isEqualToString:dicObject[@"company"]] &&
            [obj[@"product"] isEqualToString:dicObject[@"product"]]);
}];

if (indexOfDicObject == NSNotFound)
{
    [arr addObject:dicObject];
}
else
{
    NSNumber *quantity = arr[indexOfDicObject][@"quantity"];
    arr[indexOfDicObject][@"quantity"] = @([quantity intValue] + [dicObject[@"quantity"] intValue]);
}

我做了以下假设:

  • company的值是NSString;
  • product的值是NSString;
  • quantity的值是一个整数,存储在NSNumber中.
  • the company value is a NSString;
  • the product value is a NSString;
  • the quantity value is an integer, stored in a NSNumber.

另请参见 trojanfoe的答案,如果您可以按类替换字典,则更好.

See also trojanfoe's answer, which is better if you can replace your dictionaries by classes.

这篇关于如何避免NSMutableArray中的重复字典对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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