问题
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:]
?
回答1:
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.
来源:https://stackoverflow.com/questions/7472281/what-is-the-bindings-parameter-for-the-block-in-predicatewithblock-used-for