[英]Searching NSArray of NSDictionary objects
I've contacts array where each contact is a dictionary.我有联系人数组,其中每个联系人都是字典。
Each contact has a key "contact_type" which is an NSNumber.每个联系人都有一个键“contact_type”,它是一个 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.联系人类型基本上代表它是 Facebook、LinkedIn 还是邮件联系人等。我有一个搜索数组,它只包含 NSNumber 的contact_type。
What I want is to make a temporary array of contacts which I want to search using my search array.我想要的是制作一个临时的联系人数组,我想使用我的搜索数组进行搜索。
I am facing trouble using NSPredicate to create searched contacts array.我在使用 NSPredicate 创建搜索联系人数组时遇到了麻烦。 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];
After this, filteredContacts
will contain only the contacts whose contact_type
is 42
.之后, filteredContacts
将只包含contact_type
为42
的联系人。
If you need to search for more than one kind of contact_type
, then simply use an OR
in the predicate:如果您需要搜索不止一种contact_type
,那么只需在谓词中使用OR
:
filter = [NSPredicate predicateWithFormat:@"contact_type = 42 OR contact_type = 23"];
This solution work cool for me.这个解决方案对我来说很酷。
//NSDictionary Format //NSDictionary 格式
{
18=(
{
content="Great Avijit",
id=abc
}
),
13=(
{
content="Stack Overflow is cool",
id=abc
}
),
}
//Step 1 //步骤1
-(void)filterContentForSearchText:(NSString *)searchText
{
[self.filterResultArray removeAllObjects];
NSPredicate *namePredicate = [NSPredicate predicateWithFormat:@"(content contains[cd] %@)", searchText];//from_user.name
//other
for (NSMutableDictionary *key in self.messageAll) {
NSArray *array=[self.messageAll objectForKey:key];
array=[array filteredArrayUsingPredicate:namePredicate];
if ([array count]==0) {
[self.filterResultArray removeObject:key];
}
else{
if ([self.filterResultArray containsObject:key]) {
}
else{
[self.filterResultArray addObject:key];
}
}
}
//Reload table view
[self.tableView reloadData];
}
//Step 2: numberOfRowsInSection //第2步:numberOfRowsInSection
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (self.searchController.active && ![self.searchController.searchBar.text isEqualToString:@""]) {
return [self.filterResultArray count];
}else{
return [keyArray count];//keyArray hold all key of original dict
}
}
//Step 3: In cellForRowAtIndexPath method //第 3 步:在 cellForRowAtIndexPath 方法中
NSString *key = [self.filterResultArray objectAtIndex:indexPath.row];
NSDictionary *dictionary = [[self.messageAll objectForKey:key] lastObject];//self.messageAll is original dict
Dave's answer above is perfect, I am just adding here what I learned after spending 3 hours of my night sleep.上面戴夫的回答是完美的,我只是在这里添加我花了 3 个小时的夜间睡眠后学到的东西。
When you are trying to search string values within dictionary of an array yo need to use predicate something like this:当您尝试在数组的字典中搜索字符串值时,您需要使用如下谓词:
NSPredicate *filter = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"Title CONTAINS[cd] '%@' OR Description CONTAINS[cd] '%@'", searchText, searchText]];
Don't forget wrapping your values with '', this was the only thing that cost me all the time.不要忘记用''包装你的价值观,这是唯一让我付出代价的事情。 Here is my final search function这是我最后的搜索 function
-(void)searchTerm : (NSString*)searchText {
if (searchText!=nil && searchText.length) {
NSPredicate *filter = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"Title CONTAINS[cd] '%@' OR Description CONTAINS[cd] '%@'", searchText, searchText]];
self.reasonDetailArray = [self.unfilteredReasonDetailArray filteredArrayUsingPredicate:filter];
}else{
self.reasonDetailArray = self.unfilteredReasonDetailArray;
}
[self.tableView reloadData];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.