【问题标题】:Multiple NSPredicates to single Array values in iosios中的多个NSPredicates到单个数组值
【发布时间】: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


【解决方案1】:

在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 (NSMutableArray *array = [NSMutableArray array];),然后在组合 NSPredicates 时将谓词实例化为空追加。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-10
    • 2019-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-14
    相关资源
    最近更新 更多