在我的项目中,我在NSPredicate工作,我已经为一个NSPredicate工作,但现在我需要使用多个NSPredicate.Here我有一个阵列,从这个数组我希望过滤多个情况下,
我有三个这样的复选框,当我取消选择绿色按钮意味着它只显示红色&黄色值然后我取消选择黄色表示它只显示红色值我选择绿色表示它显示绿色&红色值
即
1.Color is equal or not equal to Green.
2.Color is equal or not equal to Yellow.
3.Color is equal or not equal to Red.
我的阵列有这三种颜色的组合在这里我试过,
NSPredicate *filterRun,*filterNotProd,*filterStop,*filterR;
if(isRun){
filterRun = [NSPredicate predicateWithFormat:@"Color != [c] %@",GREEN_COLOR];
}
else{
filterRun = [NSPredicate predicateWithFormat:@"Color == [c] %@",GREEN_COLOR];
}
if(isNotProd){
filterNotProd = [NSPredicate predicateWithFormat:@"Color != [c] %@",YELLOW_COLOR];
}
else{
filterNotProd = [NSPredicate predicateWithFormat:@"Color == [c] %@",YELLOW_COLOR];
}
if(isStop){
filterStop = [NSPredicate predicateWithFormat:@"Color != [c] %@",RED_COLOR];
}
else{
filterStop = [NSPredicate predicateWithFormat:@"Color == [c] %@",RED_COLOR];
}
// filterR = [NSPredicate predicateWithFormat:@"Color LIKE[c] %@",RED_COLOR];
NSLog(@"prediStr Runn %@ nnn %@ sss %@",filterRun,filterNotProd,filterStop);
NSPredicate *compFilter=[NSCompoundPredicate andPredicateWithSubpredicates:@[filterRun,filterNotProd,filterStop]];
// NSPredicate *compFilter=[NSCompoundPredicate orPredicateWithSubpredicates:@[filterRun,filterNotProd,filterStop]];
NSMutableArray *filteredArr = [NSMutableArray arrayWithArray:[parkDetailsArr filteredArrayUsingPredicate:compFilter]];
parkDetailsArr=[NSMutableArray arrayWithArray:filteredArr];
但它只显示空值..帮助我..
答案 0 :(得分:0)
在swift中,您可以为过滤数组设置AND / OR
NSPredicates,因此,您只能实例化NSCompoundPredicate
类。例如,您可以像这样组合和/或谓词:
let orPredicate:NSCompoundPredicate = NSCompoundPredicate(orPredicateWithSubpredicates: [NSPredicate(format: "petty_cash_description CONTAINS[cd] %@",str!),NSPredicate(format: "project.name CONTAINS[cd] %@",str!),NSPredicate(format: "payment_date CONTAINS %@",str!),NSPredicate(format: "amount == %f",number)])
let andPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [orPredicate,NSPredicate(format: "id != 0")])
现在您有一个自定义谓词,它将谓词与AND / OR组合在一起。在您的情况下,在函数顶部设置空NSMutableArray(NSMutableArray *array = [NSMutableArray array];
),然后在组合NSPredicates时将谓词实例化为空追加。