Multiple NSPredicate

后端 未结 3 445
情深已故
情深已故 2021-01-04 01:53

I\'m trying to prepare multiple search in my CoreData entity Recipes. There are parameters by which I would like to prepare fetch.

Recipes

相关标签:
3条回答
  • 2021-01-04 02:17

    Addition to @Joshua's answer, you can also use NSCompoundPredicate for your OR operations like this.

    Obj-C - OR

    // OR Condition //
    
    NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"X == 1"];
    NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"X == 2"];
    NSPredicate *predicate = [NSCompoundPredicate orPredicateWithSubpredicates:@[predicate1, predicate2]];
    

    Swift - OR

    let predicate1:NSPredicate = NSPredicate(format: "X == 1")
    let predicate2:NSPredicate = NSPredicate(format: "Y == 2")
    let predicate:NSPredicate  = NSCompoundPredicate(orPredicateWithSubpredicates: [predicate1,predicate2] )
    

    Swift 3 - OR

        let predicate1 = NSPredicate(format: "X == 1")
        let predicate2 = NSPredicate(format: "Y == 2")
        let predicateCompound = NSCompoundPredicate.init(type: .or, subpredicates: [predicate1,predicate2])
    
    0 讨论(0)
  • 2021-01-04 02:24

    It will work for you.

        NSPredicate *bPredicate;
        bPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@ OR product_price contains[c] %@ OR foodescription contains[c] %@",searchText,searchText,searchText];
    
    NSArray *filteredArray = [getObjectsFromServiceArray filteredArrayUsingPredicate:bPredicate];
    
    0 讨论(0)
  • 2021-01-04 02:25

    use NSCompoundPredicate for your multiple predicates, you can refer NSCompoundPredicate Class Reference

    something like this:

    NSPredicate * andPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:predicate1,predicate2,predicate3,nil]];
    
    0 讨论(0)
提交回复
热议问题