1

在我的项目中,我在 NSPredicate 工作,我已经为单个 NSPredicate 工作,但现在我需要使用多个 NSPredicate。这里我有一个数组,我想从这个数组中过滤多个案例,

我有三个这样的复选框,最初当我取消选择绿色按钮时全部选中意味着它只显示红色和黄色值然后我取消选择黄色意味着它再次只显示红色值我选择绿色意味着它显示绿色和红色值

IE

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];

但它只显示空值..帮助我..

4

1 回答 1

0

在 swift 中,您可以设置AND / ORNSPredicates 来过滤数组,因此,您只能实例化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 *array = [NSMutableArray array];在您的情况下,在函数顶部设置空 NSMutableArray ( ),然后在组合 NSPredicate 时将谓词实例化为空追加。

于 2017-07-26T15:14:14.487 回答