6

私は次のsubpredicatesと複合述語を設定したい:組み合わせる+ andPredicateWithSubpredicates:と+ orPredicateWithSubpredicates:1つの述語で

  • ID(ANDタイプ)
  • ファーストネーム(ORタイプ)
  • 姓(ORタイプ)
  • middlename(ORタイプ)

私はNSCompoundPredicate documentationを読んでいますが、enoug明確で理解していませんh - + andPredicateWithSubpredicates:+ orPredicateWithSubpredicates:の両方を1つのフェッチ要求で1つの述語として組み合わせることは可能ですか?

答えて

20

この方法を解決:

にObjC:

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

スウィフト:@ [orPredicate:

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

あなたは 'NSPredicate * finalPredicate = [NSCompoundPredicate andPredicateWithSubpredicatesに第二と第三の工程を組み合わせることができます、第3述語、第4述語]] '。 –

関連する問題