ios - 以 NSDictionary 作为对象的嵌套数组上的 NSPredicate

标签 ios objective-c nsarray nsdictionary nspredicate

我有一个像这样的 NSDictionary:

{
"2017-05-02" =     (
            {
        "always_valid" = 0;
        date = "2017-05-02";
        from = "12:00";
        to = "13:00";
    },
            {
        "always_valid" = 0;
        date = "2017-05-02";
        from = "12:00";
        to = "12:00";
    },
            {
        "always_valid" = 0;
        date = "2017-05-02";
        from = "14:00";
        "hourly_rate" = 12;
        to = "15:00";
    }
);
"2017-05-03" =     (
            {
        "always_valid" = 0;
        date = "2017-05-03";
        from = "12:00";
        to = "13:00";
    }
);
"2017-05-18" =     (
            {
        "always_valid" = 1;
        date = "2017-05-18";
        from = "12:00";
        to = "12:00";
    }
);
}

我正在尝试申请

NSPredicate *filter = [NSPredicate predicateWithFormat:@"always_valid = \"1\""];
 NSArray *alwaysvalid = [[dic allValues] filteredArrayUsingPredicate:filter];

当我有类似的结构时,它可以工作

数组>字典

但是现在好像是

数组>数组>字典

通过对数组执行 [dic allValues]。 任何帮助我应该申请什么来保持快速。

最佳答案

您需要做的是枚举您的字典并创建新的过滤字典。

NSMutableDictionary *filterDic = [[NSMutableDictionary alloc] init];
NSPredicate *filter = [NSPredicate predicateWithFormat:@"always_valid = 1"];
[dict enumerateKeysAndObjectsUsingBlock:^(NSString* key, NSArray* obj, BOOL *stop) {
     NSArray *filterArray = [obj filteredArrayUsingPredicate:filter];
     if (filterArray.count > 0) {
         filterDic[key] = filterArray;
     }
}];

关于ios - 以 NSDictionary 作为对象的嵌套数组上的 NSPredicate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43739514/

相关文章:

ios - 将 switch-as 从 Switch 转换为 Objective-C

ios - 如何使用 Parse.com 处理 NSDecimalNumber?

ios - 在 Objective-C 中如何从父类(super class)中引用子类的 View ?

iphone - 如何按整数属性对 CoreData 对象数组进行排序?

ios - 过滤其他 NSDictionary 中的 NSDictionary

ios - 如何从 NSDictionary 对象数组中找到最大值?

ios - 将对象附加到控件以指示 UITableView 中的状态

ios - Swift - 将数据从 TableView 传递到第三个 View Controller

ios - Swift 将 json 解析为 Table View

ios - 子类化 SKSpriteNode 时物理体问题?