【问题标题】:How to write predicate to filter array of dictionaries?如何编写谓词来过滤字典数组?
【发布时间】:2015-02-25 13:57:52
【问题描述】:

我有字典数组,字典包含作为 NSString 的键和作为整数的值。我尝试了以下代码,但没有得到结果。

NSPredicate *objPredicate = [NSPredicate predicateWithFormat:@"%@ = %@", key, [NSNumber numberWithInt:value]];

NSArray  *filteredArray = [array filteredArrayUsingPredicate:objPredicate];

【问题讨论】:

  • 你的字典不能有整数,只有对象可以存储在字典、数组、集合中......你的字典中必须有一个 NSNumber:这可能工作正常:[NSPredicate predicateWithFormat:@ "%@ = %@", 键, 值];

标签: ios objective-c arrays dictionary


【解决方案1】:

使用这个:

NSPredicate *objPredicate = [NSPredicate predicateWithFormat:@"self[%@] = %@", key, @(value)];

NSArray  *filteredArray = [array filteredArrayUsingPredicate:objPredicate];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    • 2020-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多