String variables are surrounded with quotation marks in predicates while dynamic properties (and hence keypaths) are not quoted. Consider this example:
Lets say we have an array of people:
NSArray *people = @[
@{ @"name": @"George", @"age": @10 },
@{ @"name": @"Tom", @"age": @15 }
];
Now if we wanted to filter our array in order to find all persons by name, we would expect a predicate that would expand to something like this:
name like[c] "George"
That way we say that name
is a dynamic key and George
is a constant string.
So, if we used a format like @"%@ like[c] %@"
the expanded predicate would be:
"name" like[c] "George"
which is clearly not what we want (here both name
and George
are constant strings)
So the correct way to build our predicate would be:
NSPredicate *p = [NSPredicate predicateWithFormat:@"%K like[c] %@", @"name", @"George"];
I hope that this makes sense. You can find much more on predicates in Apple's documentation here