如何合并两个的NSArray [英] How to merge two NSArray

查看:975
本文介绍了如何合并两个的NSArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would know how to merge two array which contain some duplicate : Here is an exemple to illustrate what I want to do :

// 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];

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

And now I have an array which contain some of those dictionaries :

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

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

And here is the new array to be merged with the first one which contain some duplicate and new "data" :

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

我的目标,在最后阶段,是有它包含了所有字典的数组所有重复删除

My goal, at the final stage, is to have an array which contains all dictionaries with all duplicate deleted

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


我不知道怎么样才是最有效的解决方案,合并确实有要快。我一定要实现快速枚举或基于块的枚举算法,或者一个NS predicate实现? 我已经搜索有关NS predicate,但我还没有找到一种方法来筛选与其它阵列= /
数组 任何帮助将是AP preciated。


I don't know how what is the most efficient solution, the merge do have to be fast. Do I have to implement a fast enumeration or a block based enumeration algorithm, or maybe a NSPredicate implementation ?. I've already search about NSPredicate but I've not found a way to filter an array with an other array =/
Any help would be appreciated.

推荐答案

您不要使用NSArrays,你需要的是一套能自动消除重复(一个杜布隆是西班牙金币)。所以,你使用的是简单的的NSMutableSet并添加NSDictionaries吧。

You do not use NSArrays, what you need is a set which automatically eliminates "duplicates" (a "doubloon" is a Spanish gold coin). So you are using simply an NSMutableSet and add your NSDictionaries to it.

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

这篇关于如何合并两个的NSArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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