【问题标题】:iOS filter array of dictionary base on value and not the keyiOS过滤器数组基于值而不是键
【发布时间】:2013-07-24 21:40:06
【问题描述】:

我有这样的字典数组:

Array: (
 {
customerUS= {
 DisplayName = "level";
 InternalName = "Number 2";
 NumberValue = 1;
 },
 customerCAN= {
 DisplayName = "PurchaseAmount";
 InternalName = "Number 1";
 NumberValue = 3500;
 };
}
)

我想根据特定值而不是键来过滤字典。例如,我想要任何键值为 3500 的所有字典。有没有人知道我该怎么做?

非常感谢您的帮助。

【问题讨论】:

标签: ios nsarray nsdictionary


【解决方案1】:

尝试如下谓词格式:

@"%@ IN SELF", testValue

当您过滤数组时,每个数组都将针对IN 运行。当您传递 IN 字典时,它会使用字典的值。

【讨论】:

  • NSPredicate *myPredicate = [NSPredicate predicateWithFormat:@"%@ in self", @"3500"];但不起作用。
  • 您的问题实际上显示了一个字典数组,准确吗(看起来不像是日志输出)?
  • 这是我得到的结果
  • 如果有嵌套字典,过滤结果应该是什么?外部数组中有多少个字典?
  • 如果字典在任何键上有 3500 应该是真的
【解决方案2】:

你也可以使用

-keysOfEntriesPassingTest:

的 NSDictionary。像这样传入一个块:

for(NSDictionary *myDictionary in myArray){ 
    NSSet *resultSet = [myDictionary keysOfEntriesPassingTest:^(id key, id object, BOOL *stop) {
        //assuming that 3500 is an int, otherwise use appropriate condition.
        if([[NSNumber numberWithInt:object] isEqual:[NSNumber numberWithInt:3500]]){
            return YES;
        }else{
            return NO;
        }
     }];
    if (resultSet.count>0){
        //do whatever to myDictionary
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-03
    • 1970-01-01
    • 2019-10-14
    • 2012-06-14
    • 1970-01-01
    • 2010-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多