开发者社区> 问答> 正文

请问怎么创建一个or和and双语句

有下面三个类型获取,需要添加一个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];

展开
收起
爵霸 2016-03-26 09:20:38 1882 0
1 条回答
写回答
取消 提交回答
  • 这应该可以解决

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

    或者使用“modern Objective-C”语法

    NSPredicate *finalPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:
                                         @[compoundPredicate, markPredicate]];
    2019-07-17 19:15:16
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载