6

voglio impostare un predicato composto con prossimi subpredicates:Combinando + andPredicateWithSubpredicates: e + orPredicateWithSubpredicates: in un predicato

  • id (e tipo)
  • cognome (o tipo)
  • cognome (OR tipo)
  • MiddleName (o tipo)

sto leggendo NSCompoundPredicate documentation ma non capisco chiaro enoug h - è possibile utilizzare entrambi i valori + andPredicateWithSubpredicates: e + orPredicateWithSubpredicates: per combinarli come un unico predicato in una richiesta di recupero?

risposta

20

risolto questo modo:

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]) 
+1

è possibile combinare la seconda e la terza fase a 'NSPredicate * finalPredicate = [NSCompoundPredicate andPredicateWithSubpredicates: @ [orPredicate , thirdPredicate, fourthPredicate]] '. –