【发布时间】:2011-12-19 19:17:41
【问题描述】:
是否可以将保留字(特别是 OR 和 AND)替换为 predicateFormat?
我试过了:
NSString *andOr = (isAnd ? @"AND" : @"OR"); // isAnd is a BOOL variable
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"firstName == %@ %K lastName == %@",
user.firstName, andOr, user.lastName];
但我得到了:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: 'Unable to parse the format string "firstName == %@ %K lastName == %@"
我尝试了%s(以及%S)而不是%K,但遇到了同样的异常。
我目前的工作解决方案是单独创建predicateFormat。
NSString *predicateFormat = [NSString stringWithFormat:
@"firstName == %%@ %@ lastName == %%@", andOr];
NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateFormat,
user.firstName, user.lastName];
但是,我想知道这是否可以在不单独创建 predicateFormat 的情况下完成。
【问题讨论】:
标签: nspredicate stringwithformat predicatewithformat