Reputation: 177
In my projects i'm working in NSPredicate i already worked for a single NSPredicate but now i need to Use multiple NSPredicate.Here i'm having one array, from this array i want to filter by the multiple cases,
I'm having three check box like that, initially all are checked when I deselect the green button means it shows only the red & yellow values then I deselect the yellow means it show only the red value again I select the green means it shows the green & red values
i.e
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.
My array having a combination of these three colors Here i tried,
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];
but it showing empty value only..help me..
Upvotes: 1
Views: 143
Reputation: 974
In swift you can set AND / OR
NSPredicates for filtering array so, you can only instantiate NSCompoundPredicate
class. For example you can combine and / or predicates like this:
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")])
Now you have a custom predicate that combines predicates with AND / OR. In your case, set the empty NSMutableArray (NSMutableArray *array = [NSMutableArray array];
) at the top of your function and then instantiate the predicates to empty append when you are combining NSPredicates.
Upvotes: 0