Searching NSArray of NSDictionary objects

2019-01-11 13:56发布

I've contacts array where each contact is a dictionary.

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.

I am facing trouble using NSPredicate to create searched contacts array. Can some one guide me on how to do it?

3条回答
smile是对你的礼貌
2楼-- · 2019-01-11 14:22
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.

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"];
查看更多
我命由我不由天
3楼-- · 2019-01-11 14:27

This solution work cool for me.

//NSDictionary Format

{
 18=(
     {
        content="Great Avijit",
        id=abc
     }
 ),
13=(
     {
        content="Stack Overflow is cool",
        id=abc
      }
 ),
} 

//Step 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

- (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

 NSString *key = [self.filterResultArray objectAtIndex:indexPath.row];
  NSDictionary *dictionary = [[self.messageAll objectForKey:key] lastObject];//self.messageAll is original dict
查看更多
神经病院院长
4楼-- · 2019-01-11 14:32

Dave's answer above is perfect, I am just adding here what I learned after spending 3 hours of my night sleep.

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

-(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];
}
查看更多
登录 后发表回答