搜索NSDray对象的NSArray [英] Searching NSArray of NSDictionary objects
问题描述
我是联系人数组,其中每个联系人都是字典。
I've contacts array where each contact is a dictionary.
每个联系人都有一个键contact_type,这是一个NSNumber。联系人类型基本上代表它是Facebook,LinkedIn还是邮件联系人等。
我的搜索数组只包含了contactNtype的NSNumber。
Each contact has a key "contact_type" which is an NSNumber. Contact type basically represents whether its a Facebook, LinkedIn or Mail contact etc. I've a search array which holds only NSNumber of contact_type's.
我想要的是制作一个临时的联系人数组,我想用我的搜索数据搜索。
What I want is to make a temporary array of contacts which I want to search using my search array.
我在使用NSPredicate创建搜索的联系人数组时遇到了麻烦。有人可以指导我如何做吗?
I am facing trouble using NSPredicate to create searched contacts array. Can some one guide me on how to do it?
推荐答案
NSArray *contacts = ...; //your array of NSDictionary objects
NSPredicate *filter = [NSPredicate predicateWithFormat:@"contact_type = 42"];
NSArray *filteredContacts = [contacts filteredArrayUsingPredicate:filter];
此后, filteredContacts
将仅包含联系人 contact_type
42
。
After this, filteredContacts
will contain only the contacts whose contact_type
is 42
.
如果您需要要搜索多种 contact_type
,然后只需在谓词中使用 OR
:
If you need to search for more than one kind of contact_type
, then simply use an OR
in the predicate:
filter = [NSPredicate predicateWithFormat:@"contact_type = 42 OR contact_type = 23"];
这篇关于搜索NSDray对象的NSArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!