Subclassing NSPredicate to add operator

为君一笑 提交于 2019-12-05 08:26:09

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.

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