简体   繁体   English

根据密钥的值从NSDictionary的NSMutableArray中删除重复项

[英]Remove duplicates from NSMutableArray of NSDictionary depending on value of key

I have a NSMutableArray of, say, 10 NSMutableArrays . 我有一个NSMutableArray ,例如10个NSMutableArrays Each of these NSMutableArrays contains different number of NSDictionary objects for which I have made a model class. 这些NSMutableArrays中的每一个都包含不同数量的NSDictionary对象,为此我制作了一个模型类。 I get this data from an API response. 我从API响应中获取此数据。 First time you make an API call, the response will be the whole data. 首次调用API时,响应将是整个数据。 From then on, when API call is made, it returns only data that is modified. 从那时起,进行API调用时,它仅返回修改后的数据。

When I get some new data from API response, it contains data that is already in the array of dictionaries but with slight modifications. 当我从API响应中获取一些新数据时,它包含的数据已经存在于字典数组中,但进行了一些修改。

Model class's structure is as below: 模型类的结构如下:

SubCategory.h 子类别

@interface SubCategory : MTLModel <MTLJSONSerializing>

@property (nonatomic, retain) NSString *subCategoryID;
@property (nonatomic, retain) NSString *subCategoryName;
@property (nonatomic, retain) NSString *status;

@end

JSON object returned will look like this: 返回的JSON对象将如下所示:

{
    "sub_category" =             (
                            {
                id = 55ed48123;
                name = "Sub Category 1";
                status = 1;
            },
                            {
                id = 55ed47ed3;
                name = "Sub Category 2";
                status = 1;
            }
      );
}

The 'id' field of new data is same. 新数据的“ id”字段相同。 But its 'name' and 'status' fields are different. 但是其“名称”和“状态”字段不同。 I want to replace the data in the NSMutableArray of NSDictionary objects with the new data so that it retains the order of the objects in this array. 我想用新数据替换NSDictionary对象的NSMutableArray中的数据,以便它保留该数组中对象的顺序。 How can I do this? 我怎样才能做到这一点?

1: Use NSPredicate to create the criteria for the duplicate condition. 1:使用NSPredicate创建重复条件的条件。

2: Use filteredArrayUsingPredicate to get an array of duplicated objects. 2:使用filteredArrayUsingPredicate获取重复对象的数组。

3: Remove one object from the duplicated array. 3:从复制的数组中删除一个对象。

4: Use removeObjectsInArray to remove the list of all objects in the duplicated array from the original array. 4:使用removeObjectsInArray从原始数组中删除重复数组中所有对象的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM