String variables are surrounded by quotation marks in predicates, while dynamic properties (and therefore key paths) are not quoted. Consider this example:
Let's say we have an array of people:
NSArray *people = @[ @{ @"name": @"George", @"age": @10 }, @{ @"name": @"Tom", @"age": @15 } ];
Now, if we want to filter our array to find all the people by name, we expect the predicate to expand like this:
name like[c] "George"
Thus, we say that name is a dynamic key, and George is a constant. So, if we used a format like @"%@ like[c] %@" , the extended predicate would be:
"name" like[c] "George"
which is clearly not what we want (here both name and George are constant lines)
Thus, the correct way to construct our predicate is as follows:
NSPredicate *p = [NSPredicate predicateWithFormat:@"%K like[c] %@", @"name", @"George"];
I hope this makes sense. You can find much more in the predicates in the Apple documentation here.
source share