需構建查詢謂詞:(category 包含 cat0 OR cat1 OR cat2) AND (mark == 1)。
NSMutableArray *orPredicates = [NSMutableArray array];
if (cat0str.length) [orPredicates addObject:[NSPredicate predicateWithFormat:@"question.category CONTAINS[c] %@", cat0str]];
if (cat1str.length) [orPredicates addObject:[NSPredicate predicateWithFormat:@"question.category CONTAINS[c] %@", cat1str]];
if (cat2str.length) [orPredicates addObject:[NSPredicate predicateWithFormat:@"question.category CONTAINS[c] %@", cat2str]];
NSPredicate *compoundOr = [NSCompoundPredicate orPredicateWithSubpredicates:orPredicates];
NSPredicate *markPred = [NSPredicate predicateWithFormat:@"question.mark == 1"];
// 組合 AND
NSPredicate *finalPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[compoundOr, markPred]];
[fetchRequest setPredicate:finalPredicate];
NSPredicate *finalPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[compoundOr, markPred]];
NSCompoundPredicate:提供 andPredicateWithSubpredicates:、orPredicateWithSubpredicates:、notPredicateWithSubpredicate:AND(OR(A,B), NOT(C))