简体   繁体   中英

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];

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.

I think you forgot "=" sign.

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

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 !

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.

You could try to initiate the NSPredicate as:

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

Hope it helps.

试试这个,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM