1

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..

Test
  • 177
  • 1
  • 1
  • 13
  • What is the test case you are running this code for. if color is green it will never be yellow or red so if your isrun isprod values are not set correctly, it might go for color is green and color is red scenario and will always give you 0. Please provide test case where you are checking it – Kapil G Jul 26 '17 at 14:46
  • I'm having three check box like that, initially all are checked when I deselect the green button means it shows only the re & 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 – Test Jul 27 '17 at 06:01

1 Answers1

0

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.

Alfredo Luco G
  • 876
  • 7
  • 18