iPhone - NSPredicate(添加更多过滤器)

标签 iphone core-data

我有这个谓词

NSPredicate *myPredicate =
   [NSPredicate predicateWithFormat:@"(date == %@) && (enabled == %@)", date, isEnabled];

在某些时候,如果 bool 测试有效,我想向该谓词添加第三个过滤器,例如

if (testValid) {
    // use some magic to add a third option here
    // the third option is "&& (paid == %@")
    // and the condition is given by isPaid
    // the idea is to append a third condition on myPredicate, making it like
    // myPredicate =
    //   [NSPredicate predicateWithFormat:@"(date == %@) && (enabled == %@) && (paid == %@)", date, isEnabled, isPaid];
}

我想我可以为该格式创建一个 NSMutableString 并在必要时附加第三部分,但我怀疑有一种方法可以以更优雅的方式使用 NSPredicate 来做到这一点。

我该怎么做?可能吗?

谢谢

最佳答案

看一下 NSCompoundPredicate,它允许您从一个或多个子谓词构造 NSPredicate 实例:

NSArray *subpreds = [NSArray arrayWithObjects:existingPredicate, [NSPredicate predicateWithFormat:...], nil];
NSPredicate *finished = [NSCompoundPredicate andPredicateWithSubpredicates:subpreds];

关于iPhone - NSPredicate(添加更多过滤器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7327941/

相关文章:

iphone - 如何使用宏检测 iPhone 操作系统版本

ios - 在 iOS 上扫描蓝牙 2.1(旧版)设备

iphone - 辞去急救人员的正确(App Store 安全)方式?

ios - 状态恢复和托管对象崩溃

iphone - 核心数据错误 "data: <fault>"

ios - 敏感信息应该存储在哪里?

iphone - 在 UILabel 中显示 float

ios - Coredata 轻量级迁移丢失数据

objective-c - 您需要实现什么来为 NSArrayController 提供内容集?

iphone - XMPP 框架 - 在后台连接的基于位置的应用程序