如何使用NSPredicate在字典数组中搜索数组中的字符串?

时间:2015-09-25 12:45:41

标签: ios nspredicate

嗨,我有一个像这样的数组:

@[
    @{@"Name":@"marko"; @"ids"=@[@"121",@"156",@"175",@"154"];},
    @{@"Name":@"shine";@"ids"=@[@"175",@"165",@"154",@"187"];},
    @{@"Name":@"David";@"ids"=@[@"857",@"297",@"156",@"254"];},
    @{@"Name":@"Harry"; @ids"=@[@"297",@"154",@"867",@"201"];},
]; 

如何使用NSPredicate在@"156"数组中获取字符串ids的字典。

1 个答案:

答案 0 :(得分:0)

试试这个:

NSDictionary *dict1 = @{@"Name": @"marko", @"ids" : @[@"121",@"156",@"175",@"154"]};
NSDictionary *dict2 = @{@"Name":@"shine", @"ids" : @[@"175",@"165",@"154",@"187"]};
NSDictionary *dict3 = @{@"Name":@"David", @"ids" : @[@"857",@"297",@"156",@"254"]};
NSDictionary *dict4 = @{@"Name":@"Harry", @"ids" : @[@"297",@"154",@"867",@"201"]};

NSArray *test = @[dict1, dict2, dict3, dict4];

NSArray *filtered = [test filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"ids CONTAINS[cd] %@", @"156"]];