有下面三個類型獲取,需要添加一個AND語句過濾結果,其中mark是使用狀態。
1.
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];
2.
NSPredicate *markPredicate =[NSPredicate predicateWithFormat:@"question.mark == 1"];
}
想要實現的是:
NSPredicate *finalPredicate = [compoundPredicate && markPredicate];
[fetchRequest setPredicate:finalPredicate];
這應該可以解決
NSPredicate *finalPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:
[NSArray arrayWithObjects:compoundPredicate, markPredicate, nil]];
或者使用“modern Objective-C”語法
NSPredicate *finalPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:
@[compoundPredicate, markPredicate]];