NSPredicate 与 NSCompoundPredicate?

Posted

技术标签:

【中文标题】NSPredicate 与 NSCompoundPredicate?【英文标题】:NSPredicate vs. NSCompoundPredicate? 【发布时间】:2011-10-21 10:47:55 【问题描述】:

使用具有“Condition1 AND condition2”之类的值的单个 NSPredicate 与使用具有两个子谓词的 NSCompoundPredicate 的优缺点是什么?

即使我动态构建它,第一个选项似乎对代码更紧凑。

【问题讨论】:

【参考方案1】:

您几乎已经回答了自己的问题 - 与其说是性能问题,不如说是代码清洁度问题!

NSCompoundPredicate 如果您要在代码的单独部分中创建大量条件然后想要组合起来,则更有用。

如果您的谓词是在一个地方创建的,只需在格式字符串中使用“AND”即可。

当你决定你的谓词足够复杂以至于创建它需要分成不同的方法时,这真的取决于你。

如果只是 a AND b 则使用单个谓词

如果是a AND (b OR c OR c) AND (e OR b),则使用复合谓词!

【讨论】:

以上是关于NSPredicate 与 NSCompoundPredicate?的主要内容,如果未能解决你的问题,请参考以下文章

NSPredicate 与 NSCompoundPredicate?

NSPredicate 与 OR 返回错误

多对多 NSPredicate 与 NSFetchedResultsController

NSPredicate 与 64 位整数?

将 NSPredicate 与字符串匹配

NSPredicate 与关系