Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the bindings parameter for the block in predicateWithBlock: used for?

The declaration for +[NSPredicate predicateWithBlock:] looks like this:

+ (NSPredicate *)predicateWithBlock:(BOOL (^)(id evaluatedObject, NSDictionary *bindings))block

Apple's documentation for the second parameter to the block, bindings, says that it is:

The substitution variables dictionary. The dictionary must contain key-value pairs for all variables in the receiver.

I can't figure out why this parameter is needed -- nor have I seen it being used anywhere. Why is it there?

Also, do I need to look inside bindings when using a block based predicate with -[NSArray filteredArrayUsingPredicate:]?

like image 433
Chaitanya Gupta Avatar asked Sep 19 '11 14:09

Chaitanya Gupta


1 Answers

See the class documentation:

You can also create predicates that include variables, so that the predicate can be pre-defined before substituting concrete values at runtime. In Mac OS X v10.4, for predicates that use variables, evaluation is a two step process (see predicateWithSubstitutionVariables: and evaluateWithObject:). In Mac OS X v10.5 and later, you can use evaluateWithObject:substitutionVariables:, which combines these steps.

Then check out the predicate syntax docs.


If you were to subsequently invoke evaluateWithObject:substitutionVariables:, said dictionary would be passed to your block. This enables rather generic predicate creation where the resulting predicate can be passed around and a consistent substitution language can be used for evaluation.

like image 174
bbum Avatar answered Nov 07 '22 01:11

bbum