I am trying to create a function that searches through my core data stack and return the amount of messages for a desired key/value.
Here is my code:
let predicate = NSPredicate(format: "SELF[%@] CONTAINS %@",key,value)
This worked for me
To substitute a key in the predicate string, you have to use %K
, not %@
:
[NSPredicate predicateWithFormat:@"%K == %@", key, value];
From the Predicate Format String Syntax documentation:
- %@ is a var arg substitution for an object value—often a string, number, or date.
- %K is a var arg substitution for a key path.