将NSPredicate子类化为add运算符 [英] Subclassing NSPredicate to add operator
问题描述
Cocoa定义了谓词类( NSPredicate
, NSExpression
等)在Cocoa中指定查询 谓词编程 。
NSComparisonPredicate $ c $这些类描述了我需要的一些东西, c>已处理14个运算符( NSPredicateOperatorType ),但我想添加,说,时间运算符...或运算符来表示诸如:
- 的值 连续天(三元运算符)
$ b b
显然,我需要自己实现这些,并且执行这些查询的数据模型将必须支持这些操作符。但是,有没有一种方法来实现它,并从现有的NSPredicate类中受益?由于运算符被定义为枚举
,我怀疑我可以在这方面延伸。
有 支出 a lot of 时间 播放 周围 与 NSPredicate
,我不确定这是最好的想法。 / p>
理论上,您将子类 NSPredicate
,创建新的初始化器和属性,然后覆盖 -evaluateWithObject:substitutionVariables:
方法进行自定义比较。
实际上,
您可以考虑使用 FUNCTION()
。 我写了一篇博客文章 FUNCTION
,以及如何使用 NSExpression
,因此使用 NSPredicate
。个人而言,我可能会用这个,因为那时你仍然可以使用 + predicateWithFormat:
语法创建 NSPredicate
。创建子类以添加运算符必然会阻止您使用内置的解析器。
Cocoa defines predicate classes (NSPredicate
, NSExpression
, etc.) which "provide a general means of specifying queries in Cocoa" Predicate Programming. This set of classes describes what I need but with one little short-coming : I'd like additional operators.
NSComparisonPredicate
already handles 14 operators (NSPredicateOperatorType) but I would like to add, say, temporal operators... or operators to represent things such as:
- " variable has at least n entries" (binary operator)
- " variable has value for, at most, n consecutive days" (ternary operator)
Obviously, I would need to implement these myself and the data model on which such queries are performed will have to support these operators. But, is there a way to implement it and benefit from the existing NSPredicate classes? Since operators were defined as an enum
, I doubt I can extend on that front. Or am I completely missing the boat on this?!
Having spent a lot of time playing around with NSPredicate
, I'm not sure this is the greatest idea.
Theoretically, you'd subclass NSPredicate
, create your new initializer and properties, and then override the -evaluateWithObject:substitutionVariables:
method to do your custom comparison.
Practically, it's probably a lot more difficult than that.
You might consider using FUNCTION()
instead. I wrote a blog post about FUNCTION
a while ago and how it plays with NSExpression
and therefore with NSPredicate
. Personally, I'd probably go with this, because then you could still use the +predicateWithFormat:
syntax to create the NSPredicate
. Creating a subclass to add an operator would necessarily prevent you from using the built-in parser.
这篇关于将NSPredicate子类化为add运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!