开发者

iPhone - NSPredicate (adding more filters)

I have this predicate

NSPredicate *myPredicate =
   [NSPredicate predicateWithFormat:@"(date == %@) && (enabled == %@)", date, isEnabled];

At some point, if a boolean test is valid, I would like to add a third filter to this predicate, something like

if (testValid) {
    // use some magic to add a third option here
    // the t开发者_运维技巧hird option is "&& (paid == %@")
    // and the condition is given by isPaid
    // the idea is to append a third condition on myPredicate, making it like
    // myPredicate =
    //   [NSPredicate predicateWithFormat:@"(date == %@) && (enabled == %@) && (paid == %@)", date, isEnabled, isPaid];
}

I suppose I can create a NSMutableString for the format and append the third part if necessary, but I suspect there's a way to do it using NSPredicate in a more elegant way.

How do I do that? Is it possible?

Thanks


Take a look at NSCompoundPredicate which lets you construct an instance of NSPredicate from one or more subpredicates:

NSArray *subpreds = [NSArray arrayWithObjects:existingPredicate, [NSPredicate predicateWithFormat:...], nil];
NSPredicate *finished = [NSCompoundPredicate andPredicateWithSubpredicates:subpreds];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜