15

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?

Abizern
  • 146,289
  • 39
  • 203
  • 257
Satyam
  • 15,493
  • 31
  • 131
  • 244

3 Answers3

37
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"];
Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
  • Hi, thanks for the response. but i'm looking for search of more than one contact type.... I found the solution. – Satyam May 01 '11 at 04:23
  • Thanks again. I posted the solution which is similar to that of yours,but more exact solution that I'm looking for. – Satyam May 01 '11 at 04:26
  • But my solution is failing ...., from your solution, I've an array of 42,43..... which I want to search and not individually as you mentioned – Satyam May 01 '11 at 04:32
  • @Satyam svv: (based on your now-deleted answer) `contact_type` returns a scalar value, and a scalar value can't "contain" anything. However, if you change `CONTAINS` to `IN`, *then* it should work. – Dave DeLong May 01 '11 at 04:34
  • When using NSNumber to compare with array, its not working. For strings its working. Following code is not working NSPredicate* predicate = [NSPredicate predicateWithFormat:@"type IN %@",postMediumIDs]; self.contactsArray = (NSMutableArray*)[self.contactsArray filteredArrayUsingPredicate:predicate]; – Satyam May 03 '11 at 17:10
  • @Satyam svv: if you ask a new question, i'd be happy to answer it. I don't have enough room in the comment box to respond fully. :) – Dave DeLong May 03 '11 at 17:15
  • Here's the new question that I posted http://stackoverflow.com/questions/5893996/iphone-problem-with-nspredicate-during-search – Satyam May 05 '11 at 07:16
  • shouldn't it be `==` instead of `=`? – Adil Soomro Jan 10 '13 at 08:30
  • how to search this type [ { company_id: 1, company_news: [ { company_id: "123", company_name: "test", }, { company_id: "123", company_name: "test", } ], [ { company_id: 1, company_news: [ { company_id: "123", company_name: "test", }, { company_id: "123", company_name: "test", } ] – Patel Maulik Feb 12 '16 at 06:21
1

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
Avijit Nagare
  • 8,482
  • 7
  • 39
  • 68
0

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];
}
rptwsthi
  • 10,094
  • 10
  • 68
  • 109