Creating an NSPredicate with Substitution Variables

suggest change

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

Objective-C

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

Swift

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.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents