如何使用复合NSPredicate?

时间:2014-04-12 03:32:50

标签: ios objective-c nspredicate

我想知道如何使用复合NSPredicate?

我已尝试如下,但是currentInstall数组在开始时与应用谓词之后完全相同。

NSArray *currentInstall = [coreDataController filterReadInstalls:selectedInstallID];
    NSArray *tempArray = [currentInstalls filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"cHR == 0"]];
    currentInstalls = [tempArray copy];

    NSPredicate *predicateAreaString = [NSPredicate predicateWithFormat:@"area == %@", [myFilter objectForKey:@"area"]];
    NSPredicate *predicateBString = [NSPredicate predicateWithFormat:@"stage == %@", [myFilter objectForKey:@"area2"]];
    NSPredicate *predicateCString = [NSPredicate predicateWithFormat:@"partCode == %@", [myFilter objectForKey:@"area3"]];
    NSPredicate *predicateDString = [NSPredicate predicateWithFormat:@"doorNo CONTAINS[cd] %@", [myFilter objectForKey:@"door"]];
    NSPredicate *predicateEString = [NSPredicate predicateWithFormat:@"doorDesc CONTAINS[cd] %@", [myFilter objectForKey:@"doorDesc"]];

    NSPredicate *compoundPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:@[predicateAreaString, predicateBString, predicateCString, predicateDString, predicateEString]];

    NSMutableArray *filteredArray = [NSMutableArray arrayWithArray:[currentInstalls filteredArrayUsingPredicate:compoundPredicate]];
    currentInstalls = [filteredArray mutableCopy];

1 个答案:

答案 0 :(得分:1)

您实施NSCompundPredicate的方式似乎没有任何明显错误。如果你没有尝试And或Not谓词,那么我会说你的谓词格式有问题,以及它们如何匹配你正在过滤的数组。

我会尝试仅使用其中两个谓词来创建NSCompundPredicate然后让它工作或查看导致问题的原因。 NSHipster还有一些关于NSPredicates的好消息。