iOS Creating an NSPredicate with Substitution Variables


An NSPredicate can use substitution variables to allow values to be bound on the fly.


NSPredicate *template = [NSPredicate predicateWithFormat: @"self BEGINSWITH $letter"];
NSDictionary *variables = @{ @"letter": @"r" };
NSPredicate *beginsWithR = [template predicateWithSubstitutionVariables: variables];


let template = NSPredicate(format: "self BEGINSWITH $letter")
let variables = ["letter": "r"]
let beginsWithR = template.predicateWithSubstitutionVariables(variables)

The template predicate is not modified by predicateWithSubstitutionVariables. Instead, a copy is created, and that copy receives the substitution variables.