pawelini1
pawelini1

Reputation: 487

Strange behavior of NSPredicate,

Why does this predicate returns results:

predicate= [NSPredicate predicateWithFormat:@"itemsString like '*{4160366}*'"];

while this predicate returns empty array

predicate= [NSPredicate predicateWithFormat:@"itemsString like '*{%@}*'",[NSString stringWithString:@"4160366"]];

I's driving me crazy

Upvotes: 0

Views: 117

Answers (1)

lnafziger
lnafziger

Reputation: 25740

When building a predicate, predicateWithFormat automatically adds quotes when performing variable substitution. So your (second) predicate ends up looking like this:

itemsString like '*{"4160366"}*'".

Notice the extra set of quotes.

Change it to:

predicate= [NSPredicate predicateWithFormat:@"itemsString like %@",[NSString stringWithFormat:@"*{%@}*", @"4160366"]];

and it should work.

(Note that instead of using stringWithString I just used @"" which does the same thing.)

Upvotes: 1

Related Questions