我的复合谓词应如何构成包容性

时间:2015-06-29 18:43:03

标签: ios objective-c nspredicate

我正在为我的应用程序中的任务列表构建一个过滤器,当我尝试组合所有过滤器时,我看不到任何数据。

预期的结果是,如果我要选择所有过滤器,我会看到我的整个数据集。

以下是过滤器的简化版本 - 通过选择完整和不完整的任务状态来说明问题。

2015-06-29 14:33:49.611 TasksApp[32174:1144416] ADD FILTER FOR INCOMPLETE TASKS
2015-06-29 14:33:49.612 TasksApp[32174:1144416] ADD FILTER FOR COMPLETE TASKS
2015-06-29 14:33:49.613 TasksApp[32174:1144416] TEMP ARRAY COUNT: 5
2015-06-29 14:33:49.613 TasksApp[32174:1144416] LOCAL RESULTS FILTERED:     (
)

我正在使用以下代码创建和添加复合过滤器:

if ([[[defaults dictionaryRepresentation] allKeys] containsObject:@"myTasksIncomplete"]) {
    if([defaults boolForKey:@"myTasksIncomplete"]){
        NSLog(@"ADD FILTER FOR INCOMPLETE TASKS");
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(Status == %@)", @"Incomplete"];
        [predicatesArray addObject:predicate];
    }
}

if ([[[defaults dictionaryRepresentation] allKeys] containsObject:@"myTasksComplete"]) {
    if([defaults boolForKey:@"myTasksComplete"]){
        NSLog(@"ADD FILTER FOR COMPLETE TASKS");
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(Status == %@)", @"Complete"];
        [predicatesArray addObject:predicate];
    }
}

if ([[[defaults dictionaryRepresentation] allKeys] containsObject:@"myTasksToday"]) {
    if([defaults boolForKey:@"myTasksToday"]){
        //not setup yet
    }
}


//All of the predicates
NSLog(@"TEMP ARRAY COUNT: %li", [tempArray count]);
NSPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:predicatesArray];
self.localResults = [[tempArray filteredArrayUsingPredicate:compoundPredicate] mutableCopy];
NSLog(@"LOCAL RESULTS FILTERED: %@", self.localResults);

1 个答案:

答案 0 :(得分:2)

使用NSCompoundPredicate制作+[NSCompioundPredicate andPredicateWithSubpredicates:]时,传递每个次级预测的项目。您可能需要+[NSCompioundPredicate orPredicateWithSubpredicates:],它会传递通过任何次级预测的项目。