如何合并两个NSArray

我会知道如何合并两个包含一些重复的数组:下面是一个例子来说明我想要做什么:

// Here is some dictionary which contain an unique "id" key. NSDictionary *dico1 = [NSDictionary dictionaryWithObjectsAndKeys: @"11111111", @"id", nil]; NSDictionary *dico2 = [NSDictionary dictionaryWithObjectsAndKeys: @"22222222", @"id", nil]; NSDictionary *dico3 = [NSDictionary dictionaryWithObjectsAndKeys: @"33333333", @"id", nil]; NSDictionary *dico4 = [NSDictionary dictionaryWithObjectsAndKeys: @"44444444", @"id", nil]; NSDictionary *dico5 = [NSDictionary dictionaryWithObjectsAndKeys: @"55555555", @"id", nil]; // And here is duplicates of the 2nd and 3td dictionary. NSDictionary *dico2_bis = [NSDictionary dictionaryWithObjectsAndKeys: @"22222222", @"id", nil]; NSDictionary *dico3_bis = [NSDictionary dictionaryWithObjectsAndKeys: @"33333333", @"id", nil]; 

现在我有一个包含这些字典的数组:

 NSArray *currentArray = [NSArray arrayWithObjects: dico1, dico2, dico3, nil]; 

这里是新的数组与第一个合并,其中包含一些重复的和新的“数据”:

 NSArray *tempArray = [NSArray arrayWithObjects: dico2_bis, dico3_bis, dico4, dico5, nil]; 

我的目标,在最后阶段,是有一个数组,其中包含所有重复删除的所有字典

 NSArray *finalArray = [NSArray arrayWithObjects: dico1, dico2, dico3, dico4, dico5, nil]; 

我不知道什么是最有效的解决scheme,合并必须快速。 我是否必须实现快速枚举或基于块的枚举algorithm,或者可能是NSPredicate实现? 我已经search了NSPredicate,但我还没有find一种方法来过滤一个数组与其他数组= /
任何帮助,将不胜感激。

你不使用NSArrays,你需要的是一套自动消除“重复”(一个“doubloon”是西class牙金币)。 所以你只是使用一个NSMutableSet并添加你的NSDictionaries。

 NSMutableSet aSet = [NSMutableSet new]; [aSet addObject:dico1]; .... 

尝试这个。

 NSArray *hasDuplicates = [NSArray arrayWithObjects:@"first", @"second", @"first", nil]; NSArray *noDuplicates = [[NSSet setWithArray: hasDuplicates] allObjects]; NSLog(@"%@ %@",hasDuplicates,noDuplicates); 

你可以使用NSMutableSet来代替NSArray。 NSSet类有一个方法,如* – (void)setSet:(NSSet )otherSet 。 NSSet的(和NSMutableSet的)testing是否双重对象。