Searching dictionary in array using predicate

I have an array of dictionaries and I want to find the dictionary with desired value for key "guid". Can not get it with this code:

NSPredicate *filter = [NSPredicate predicateWithFormat:@"guid = %@", key];
NSArray *filteredContacts = [selectedValue filteredArrayUsingPredicate:filter];

Upvotes: 3

Views: 5086

Answers (6)

yogesh wadhwa
yogesh wadhwa

Reputation: 721

Try this.

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"Name CONTAINS[cd] %@",searchBar.text];

filterdArray = [[hospitalArray filteredArrayUsingPredicate:predicate] mutableCopy];

Where Name is the key contained in the dictionary under the array.

Upvotes: 5

Javito_009
Javito_009

Reputation: 40

You could try to initiate the NSPredicate as:

NSPredicate *filter = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"guid = '%@'", key]];

Hope it helps.

Upvotes: 0

utkal patel
utkal patel

Reputation: 1421

Try this,

NSArray *filtered = [array_with_dict filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(guid == %@)", @"desired value"]];

Upvotes: 0

Urmi
Urmi

Reputation: 344

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(guid CONTAINS[c] %@)",key];
NSArray *array = [[NSArray arrayWithArray:sampleArray] filteredArrayUsingPredicate: predicate];

in above code "fromDate" is key of dictionary.

i hope this works for you.

Upvotes: 0

Kevin Machado
Kevin Machado

Reputation: 4187

The operator condition has been forgotten ;)

You need to add a double "=" like this :

NSPredicate *filter = [NSPredicate predicateWithFormat:@"guid == %@", key];
NSArray *filteredContacts = [selectedValue filteredArrayUsingPredicate:filter];

I hope this can help you !

Upvotes: 0

Pratik Mistry
Pratik Mistry

Reputation: 2945

I think you forgot "=" sign.

 NSPredicate *filter = [NSPredicate predicateWithFormat:@"guid == %@", key];
 NSArray *filteredContacts = [selectedValue filteredArrayUsingPredicate:filter];

Upvotes: 1

Related Questions