我正在使用NSPredicate过滤多级NSArray。我正在尝试使用谓词过滤数组的第3个子项,但我得到了这个

时间:2016-09-20 09:56:45

标签: ios objective-c multidimensional-array filter nspredicate

无法解析格式字符串“json.option_titles.ANY.options.ANY.jobID ==%@

虽然我可以使用for循环,但我可能有长度的选项标题,所以它不会很好。是否有可能使用nspredicate? 这是我的样本数据

(
{
    dept = 1194;
    id = 2299;
    job = 22048;
    json =     {
        name = tester;
        "option_titles" =         (
                        {
                "custom_option_title" = "";
                "financing_option" = "";
                options =                 (
                                        {
                        hours = "2.00";
                        jobID = 22048;
                        optionDesc = "Sample Description";
                    },
                                        {
                        hours = "2.00";
                        jobID = 22048;
                        optionDesc = "Sample Description";
                    },
                );

            }
        );
        "system_observations" = "";
    };
    name = tester;
    "sort_order" = 178;
    "system_observations" = "";
},
{
    dept = 1195;
    id = 2300;
    job = 22000;
    json =     {
        name = tester second;
        "option_titles" =         (
                        {
                "custom_option_title" = "title option";
                "financing_option" = "";
                options =                 (
                                        {
                        hours = "2.00";
                        jobID = 22000;
                        optionDesc = "Sample Description";
                    },
                                        {
                        hours = "2.00";
                        jobID = 22000;
                        optionDesc = "Sample Description";
                    },
                );

            }
        );
        "system_observations" = "";
    };
    name = tester;
    "sort_order" = 179;
    "system_observations" = "";
}
)

尝试过的代码: -

 NSArray *allJobs = [self getAllJobs];
                allJobs = [allJobs filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(dept_id == %@)",deptId]];

                NSMutableArray *coumpoArray = [NSMutableArray new];
                for (NSDictionary *job in allJobs) {
                    NSPredicate *predicateDeepFilterJobID = [NSPredicate predicateWithFormat:@"json.option_titles.options.jobID == %@",job[@"id" ]];
                    [coumpoArray addObject:predicateDeepFilterJobID];
                }

                NSPredicate *pr = [NSCompoundPredicate orPredicateWithSubpredicates:coumpoArray];
                filterBydept = [arrayTemplate filteredArrayUsingPredicate:pr];

尝试了另一个谓词: -

 NSArray *allJobs = [self getAllJobs];
                allJobs = [allJobs filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(dept_id == %@)",deptId]];

                NSMutableArray *coumpoArray = [NSMutableArray new];
                for (NSDictionary *job in allJobs) {
                    NSPredicate *predicateDeepFilterJobID = [NSPredicate predicateWithFormat:@"json.option_titles.ANY.options.ANY.jobID == %@",job[@"id" ]];
                    [coumpoArray addObject:predicateDeepFilterJobID];
                }

                NSPredicate *pr = [NSCompoundPredicate orPredicateWithSubpredicates:coumpoArray];
                filterBydept = [arrayTemplate filteredArrayUsingPredicate:pr];

0 个答案:

没有答案