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

二次信任 提交于 2020-01-01 07:32:22

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!