Combining + andPredicateWithSubpredicates: and + orPredicateWithSubpredicates: in one predicate

I want to set up a compound predicate with the following sub-presentations:

  • id (and type)
  • name (type OR)
  • lastname (type OR)
  • middlename (type OR)

I read the NSCompoundPredicate documentation , but I don’t understand quite clearly - is it possible to use both + andPredicateWithSubpredicates: and + orPredicateWithSubpredicates: combine them as one predicate into a single query?

+6
source share
1 answer

It is solved as follows:

ObjC:

 NSPredicate *orPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:@[firstPredicate, secondPredicate]]; NSPredicate *andPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[thirdPredicate, fourthPredicate]]; NSPredicate *finalPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[orPredicate, andPredicate]]; [fetchRequest setPredicate:finalPredicate]; 

Swift:

 let orPredicate = NSCompoundPredicate(orPredicateWithSubpredicates: [firstPredicate, secondPredicate]) let andPredicate = NSCompoundPredicate(andPredicateWithSubpredicates: [thirdPredicate, fourthPredicate]) fetchRequest.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [orPredicate, andPredicate]) 
+20
source

Source: https://habr.com/ru/post/952939/


All Articles