NSPredicate在以NSDictionary为对象的嵌套数组上 [英] NSPredicate on nested array with NSDictionary as object

查看:87
本文介绍了NSPredicate在以NSDictionary为对象的嵌套数组上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个NSDictionary,例如:

i have a NSDictionary like:

{
"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];

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

it use to work when i had structure something like

array>字典

但现在就像

array> array>字典

array > array > dictionary

通过对数组执行[dic allValues]. 任何帮助我应该采取什么措施来使其保持快速运转.

by doing [dic allValues] for array. any help what should i apply to keep it fast.

推荐答案

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

What you need to do is need enumerate your dictionary and create new filtered Dictionary.

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;
     }
}];

这篇关于NSPredicate在以NSDictionary为对象的嵌套数组上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆