简体   繁体   English

如何比较和删除2 NSMutableArray中的公共对象(NSDictionaries)?

[英]How to compare and remove common objects( NSDictionaries) from 2 NSMutableArray?

I have two NSMutableArrays, filled with dictionary objects, like 我有两个NSMutableArrays,填充了字典对象,如

NSMutableArray * bigArray = @[dictionary1,dictionary2,dictionary3];
NSMutableArray * smallArray = @[dictionary1];

I want to remove the common elements (dictionay1) from the big array 我想从大数组中删除公共元素(dictionay1)

Note: small array is subset of big array and elements are distinct 注意:小数组是大数组的子集,元素是不同的

[bigArray removeObjectsInArray:smallArray];

删除bigArray中包含的smallArray中的所有对象。

If each element is distinct (as you say in the comments) then you can filter the array using a predicate that removes all the objects that inside of the smaller subset. 如果每个元素都是不同的(正如您在注释中所说的那样),那么您可以使用谓词过滤数组,该谓词将删除较小子集内的所有对象。 Since you are using an array in your question, I assume that the order of the objects matter. 由于您在问题中使用数组,我认为对象的顺序很重要。

If you need to preserve the order, then you will need to filter the array. 如果您需要保留订单,则需要过滤数组。 Converting to a set will break the order (unless you are able to resort it afterwards. 转换为集合将破坏订单(除非您之后可以使用它。

You are using mutable arrays in your question, so I will do the same. 你在问题中使用可变数组,所以我也会这样做。 Just be aware that the original array is actually modified: 请注意,原始数组实际上已被修改:

NSMutableArray *original = [NSMutableArray arrayWithArray:@[@"A", @"E", @"C", @"B", @"D"]];
NSMutableArray *subset   = [NSMutableArray arrayWithArray:@[@"C", @"B"]];

NSLog(@"before : %@", original);

[original filterUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
    // return YES for objects to keep
    return ![subset containsObject:evaluatedObject];
}]];

NSLog(@"after : %@", original);

The output of this code is: 此代码的输出是:

before : (
    A,
    E,
    C,
    B,
    D
)
after : (
    A,
    E,
    D
)

You can see that the order is preserved. 您可以看到订单已保留。


If you don't want to modify the original array you can produce a new filtered array instead. 如果您不想修改原始数组,则可以生成新的过滤数组。 The small difference is that you call filteredArrayUsingPredicate: instead of filterUsingPredicate: . 不同的是你调用filteredArrayUsingPredicate:而不是filterUsingPredicate: . The predicate is the same: 谓词是一样的:

NSArray *filtered = [original filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
    // return YES for objects to keep
    return ![subset containsObject:evaluatedObject];
}]];

NSSet is perfect class to do this kind of job. NSSet是完成这类工作的完美课程。 Try that; 试试吧;

NSMutableSet *bigSet = [NSMutableSet setWithArray:bigArray];
[bigSet minusSet:[NSMutableSet setWithArray:smallArray]];
bigArray = [[bigSet allObjects] mutableCopy];

The thing worth to point is that if your array will store duplicate and you convert it to NSSet the duplicate will be removed in that case you shouldn't use it. 值得指出的是,如果你的数组将存储重复并将其转换为NSSet,那么副本将被删除,在这种情况下你不应该使用它。 The second thing is it can change order of the elements in your array. 第二件事是它可以改变数组中元素的顺序。

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

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