2 for 2012 for_2012 于 2013.01.17 10:47 提问

如何创建一个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];

1个回答

gaoXxxing
gaoXxxing   2013.01.17 11:23
已采纳

这应该可以解决

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

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

NSPredicate *finalPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:
                                     @[compoundPredicate, markPredicate]];
for_2012
for_2012 多谢
接近 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!