【发布时间】:2017-07-26 14:36:19
【问题描述】:
在我的项目中,我在 NSPredicate 工作,我已经为单个 NSPredicate 工作,但现在我需要使用多个 NSPredicate。这里我有一个数组,我想从这个数组中过滤多个案例,
我有三个这样的复选框,最初当我取消选择绿色按钮时全部选中意味着它只显示红色和黄色值然后我取消选择黄色意味着它再次只显示红色值我选择绿色意味着它显示绿色和红色值
即
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];
但它只显示空值..帮助我..
【问题讨论】:
-
您运行此代码的测试用例是什么。如果颜色为绿色,则永远不会为黄色或红色,因此如果您的 isrun isprod 值设置不正确,则可能适用于颜色为绿色且颜色为红色的场景,并且始终为您提供 0。请提供您正在检查的测试用例
-
我有三个这样的复选框,最初当我取消选择绿色按钮时所有都被选中意味着它只显示 re & 黄色值然后我取消选择黄色意味着它再次只显示红色值我选择绿色表示它显示绿色和红色值
标签: ios nspredicate nscompoundpredicate