我不能同时在谓词格式中使用两个运算符

时间:2013-11-27 04:50:43

标签: ios iphone ios6 nspredicate

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES[cd] %@ AND (NOT (SELF MATCHES[cd] %@))", [NSString stringWithFormat: @".*\\b%@.*", string],[NSString stringWithFormat: @".*\\[\\b%@.*", string]];

我的代码有什么问题?

1 个答案:

答案 0 :(得分:0)

尝试使用子查询功能,这对你有帮助

[NSPredicate predicateWithFormat@"(add == %@) AND (SUBQUERY(subs, $x, $x.numbervalue == %@ or $x.numbervalue == %@).@count > 0)", @"add",number1, number2];

另:

AND& OR可以在谓词格式字符串中使用,以创建复合predicates。但是,使用NSCompoundPredicate可以实现同样的目的。

例如,以下谓词是等效的:

[NSCompoundPredicate andPredicateWithSubpredicates:@[[NSPredicate predicateWithFormat:@"age > 25"], [NSPredicate predicateWithFormat:@"firstName = %@", @"Raja"]]];

[NSPredicate predicateWithFormat:@"(age > 25) AND (firstName = %@)", @"Raja"];

来源: Mattt Thompson