简体   繁体   English

使用谓词在数组中搜索字典

[英]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". 我有一系列字典,我想找到具有键“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. 其中Name是数组下字典中包含的键。

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. 在上面的代码“fromDate”是字典的关键。

i hope this works for you. 我希望这适合你。

You could try to initiate the NSPredicate as: 您可以尝试启动NSPredicate:

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

Hope it helps. 希望能帮助到你。

试试这个,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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