从字典数组中搜索值

时间:2017-01-03 12:03:17

标签: objective-c nsmutablearray uisearchbar nspredicate

我有NSMutableArray个词典: 类似的东西:

(
{
    "Key1" = "Name1";
    "Key2" = "Age1";
    "Key3" = "Address1";
 },
{
    "Key1" = "Name2";
    "Key2" = "Age2";
    "Key3" = "Address2";
},
{
    "Key1" = "Name3";
    "Key2" = "Age3";
    "Key3" = "Address3";
}
)

使用此代码搜索键1值。 用户可以键入偶数字母,我必须使用包含该字母的键搜索所有字典。

NSPredicate *filter = [NSPredicate predicateWithFormat:@"'Key1' =[cd] %@", searchstring];
NSArray *filteredContacts = [DisplayArray filteredArrayUsingPredicate:filter];

但是这个谓词不起作用。请帮忙。

1 个答案:

答案 0 :(得分:6)

尝试并使用以下代码进行检查。

contains[c]中使用NSPredicate,如下所示:

NSPredicate *filter = [NSPredicate predicateWithFormat:@"Key1 contains[c] %@ ",searchstring];
NSArray *filteredContacts = [DisplayArray filteredArrayUsingPredicate:filter];