Using Predicates
Matching an exact string
let fetchRequest = NSFetchRequest(entityName: "Foo")
var thePredicate: NSPredicate?
thePredicate = NSPredicate(format: "message == 'example'")The entity
Foohas amessagestring attribute
Substitutions
Rather than passing a static string as a predicate’s criteria. It is possible to substitute values by using format specifiers. There are five format specifiers:
%Kis a var arg substitution for a key path.%@is a var arg substitution for an object value-often a string, number, date, or an array.%ldis a var arg substitution for an int value.%lais a var arg substitution for a double.%ais a var arg substitution for a float.
In the following example, the %K format specifier serves as the left-hand argument which passes in the “message” property dynamically. The %@ format specifier serves as the right-hand argument to dynamically pass in a string containing the word “example”.
let predicate = NSPredicate(format:"%K == %@", "message", "example")