6

Chcę ustawić predykat złożony z najbliższych subpredicates:Łącząc + andPredicateWithSubpredicates: i + orPredicateWithSubpredicates: w jednym orzecznika

  • ID (i rodzaj)
  • Book Nazwa (lub wpisz)
  • lastName (OR typ)
  • drugie imię (lub wpisz)

czytam NSCompoundPredicate documentation ale nie rozumieją jasne enoug h - czy w ogóle można używać zarówno + andPredicateWithSubpredicates: i + orPredicateWithSubpredicates:, aby połączyć je jako jeden predykat w jednym żądaniu pobierania?

Odpowiedz

20

rozwiązana w ten sposób:

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

można połączyć drugi i trzeci krok do 'NSPredicate * finalPredicate = [NSCompoundPredicate andPredicateWithSubpredicates: @ [orPredicate , thirdPredicate, fourthPredicate]] '. –

Powiązane problemy