Is it possible to substitute reserved words, specifically OR
and AND
, into a predicateFormat
?
I tried:
NSString *andOr = (isAnd ? @"AND" : @"OR"); // isAnd is a BOOL variable
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"firstName == %@ %K lastName == %@",
user.firstName, andOr, user.lastName];
But I got:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: 'Unable to parse the format string "firstName == %@ %K lastName == %@"
I tried %s
(and also %S
) instead of %K
but got the same exception.
My working solution for now is to create the predicateFormat
separately.
NSString *predicateFormat = [NSString stringWithFormat:
@"firstName == %%@ %@ lastName == %%@", andOr];
NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateFormat,
user.firstName, user.lastName];
But, I was wondering if this could be done without creating predicateFormat
separately.