在predicateWithBlock中的块的bindings参数是什么? [英] What is the bindings parameter for the block in predicateWithBlock: used for?

查看:678
本文介绍了在predicateWithBlock中的块的bindings参数是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

+ [NSPredicate predicateWithBlock:] 的声明如下所示:

  +(NSPredicate *)predicateWithBlock:(BOOL(^)(id calculatedObject,NSDictionary * bindings))块


$ b b

Apple的文档中对块的第二个参数 bindings 表示它是:


替换变量字典。字典必须包含接收器中所有变量的键值对。


我不知道为什么需要这个参数 - - 也没有我看到它被用在任何地方。为什么会有?



此外,当使用基于块的谓词时,我需要查看 bindings code> - [NSArray filteredArrayUsingPredicate:] ?

解决方案

/ p>


您还可以创建包含变量的谓词,以便可以在将具体值替换为$ b之前预定义
谓词$ b运行时。在Mac OS X v10.4中,对于使用变量的谓词,
求值是一个两步过程(参见
predicateWithSubstitutionVariables:和evaluateWithObject :)。在Mac
OS X v10.5及更高版本中,您可以使用
evaluateWithObject:substitutionVariables :,它结合了这些步骤。



$ b b

然后查看谓词语法文档






如果您以后调用 evaluateWithObject:substitutionVariables:,所述字典将被传递给你的块。这使得相当通用的谓词创建,其中可以传递得到的谓词,并且可以使用一致的替换语言进行评估。


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:]?

解决方案

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.

这篇关于在predicateWithBlock中的块的bindings参数是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆