0

I have array of dictionaries like this:

clients (
        {
        country = canada;
        city = vancouver;
    },
{
        country = Mexico;
        city = Mexico;
    },
{
        country = UK;
        city = London;
    },
{
        country = USA;
        city = NewYork;
    },
}

Where if I use NSPredicate to find the clients in USA like this:

NSPredicate *usaPredicte = [NSPredicate predicateWithFormat:@"SELF contains %@", @"USA"];
self.usa = [listOfClients filteredArrayUsingPredicate:usaPredicte];

works just fine but if for some reason the result of the predicte is null for example in this case:

NSPredicate *peruPredicte = [NSPredicate predicateWithFormat:@"SELF contains %@", @"Peru"];
self.peru = [listOfClients filteredArrayUsingPredicate:peruPredicte];

I get this error:

-[__NSCFDictionary filteredArrayUsingPredicate:]: unrecognized selector sent to instance 0xdcb6e0

Any of you knows why or how can avoid having this error when the predicate is empty or null?

I'll really appreciate your help

Alexander
  • 8,117
  • 1
  • 35
  • 46
  • 1
    Are you by any chance reusing the `listOfClients` pointer for something else between the two calls? – Alexander Oct 16 '13 at 19:21
  • the listofClients is intact between calls – user2887727 Oct 16 '13 at 19:31
  • when a variable changes to a different object that's often a sign that there is a problem with memory management. You should [check for zombies](http://stackoverflow.com/questions/2190227/how-do-i-set-up-nszombieenabled-in-xcode-4/4917557#4917557) – Matthias Bauch Oct 16 '13 at 20:20
  • `NSPredicate *usaPredicte = [NSPredicate predicateWithFormat:@"country contains %@", @"USA"]; self.usa = [listOfClients filteredArrayUsingPredicate:usaPredicte];` as you are looking for `Country` in Dictionary of Array so use your key which you want to filter. replace your code. Good Luck – Dipen Panchasara Oct 17 '13 at 08:57
  • self.peru is a "NSDictionary"? – TonyMkenu Oct 17 '13 at 09:35

1 Answers1

0

try this

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(country ==  %@)", @"USA"];

NSArray *filteredArray = [listOfClients filteredArrayUsingPredicate:predicate];

Assuming filterArray always have count 1.

if (filteredArray.count ) 
     self.usa = [[filteredArray objectAtIndex: 0] objectForKey: @"country"];
Rocker
  • 1,269
  • 7
  • 15