NSPredicate 与 NSCompoundPredicate?
使用具有“Condition1 AND condition2”之类的值的单个 NSPredicate 与使用具有两个子谓词的 NSCompoundPredicate 有何优缺点?
即使我动态构建它,第一个选项对于代码来说似乎更加紧凑。
What are the pros and cons for using a single NSPredicate with a value like "Condition1 AND condition2" vs. using a NSCompoundPredicate with two subpredicates?
Even if I construct it dynamically, the first option seems much more compact to code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您几乎已经回答了自己的问题 - 这更像是代码的整洁问题而不是性能问题!
如果您在代码的不同部分中创建大量条件然后想要组合,则
NSCompoundPredicate
会更有用。如果您的谓词全部创建在一处,只需在格式字符串中使用“AND”即可。
当您决定您的谓词足够复杂以至于需要将创建它分成单独的方法时,这实际上取决于您。
即,
如果它只是
a AND b
,则使用单个谓词如果它是
a AND (b OR c OR c) AND (e OR b)
,则使用复合谓词!You've almost answered your own question - it's more a cleanliness of code question than a performance question!
NSCompoundPredicate
is more useful if you are creating lots of conditions in seperate parts of your code that you then want to combine.If your predicate is being created all in one place, just use ' AND ' in a format string.
It's really up to you when you decide that your predicate is complicated enough that creating it needs to be split into separate methods.
i.e.
If it's just
a AND b
then use a single predicateIf it's
a AND (b OR c OR c) AND (e OR b)
then use compound predicates!