我获取了以下 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/