开发者

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 p开发者_如何学运维arameter 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:]?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜