iphone - 如何使用 "OR"和 "AND"创建一个 compoundPredicate

标签 iphone ios

我获取了以下 3 个类别,但想添加一个“AND”谓词来缩小结果范围,其中“标记”处于打开状态。

NSMutableArray *questionNumberArray=[[NSMutableArray alloc] init];

// Fetch questions
NSManagedObjectContext *context = self.document.managedObjectContext;
NSFetchRequest *fetchRequest =
[[NSFetchRequest alloc] initWithEntityName:@"Questions"];

//building the predicate with selected category
NSMutableArray *parr = [NSMutableArray array];

if ([cat0str length]){
    [parr addObject:[NSPredicate predicateWithFormat:@"question.category CONTAINS[c] %@",cat0str]];
}
if ([cat1str length]){
    [parr addObject:[NSPredicate predicateWithFormat:@"question.category CONTAINS[c] %@",cat1str]];
}
if ([cat2str length]){
    [parr addObject:[NSPredicate predicateWithFormat:@"question.category CONTAINS[c] %@",cat2str]];
}

NSPredicate *compoundPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:parr];

[fetchRequest setPredicate:compoundPredicate];

    NSPredicate *markPredicate =[NSPredicate predicateWithFormat:@"question.mark == 1"];
}

//我想做这样的事情:

NSPredicate *finalPredicate = [compoundPredicate && markPredicate];

[fetchRequest setPredicate:finalPredicate];

最佳答案

这是您要找的吗?

NSPredicate *finalPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:
         [NSArray arrayWithObjects:compoundPredicate, markPredicate, nil]];

或者,对容器文字使用“现代 Objective-C”语法:

NSPredicate *finalPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:
                                     @[compoundPredicate, markPredicate]];

关于iphone - 如何使用 "OR"和 "AND"创建一个 compoundPredicate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14366403/

相关文章:

android - 在多个 iOS 和 Android 设备之间保持数据库同步的最简单方法是什么?

android - Android上的任务队列就像iOS上的GCD一样?

iphone - 在 float 和 int 值的情况下如何检查 NSArray 对象的数据类型?

c# - 未经授权的访问异常 : Access to the path [ iOS/Unity3D ]

ios - 错误 : WatchKit apps must have a deployment target equal to iOS 8. 2(原为 8.3)

ios - UITableViewCell 从初始大小到全屏的高度

ios - 在设备上运行测试版 iOS 应用程序时出错

ios - 如何使用委托(delegate)将用户的输入保存在变量中?

iphone - iOS - 尝试旋转文本标签并且标签消失

iphone - Youtube 视频崩溃 Xcode ios6