结合+和PredredWithSubpredicates:和+ orPredicateWithSubpredicates:在一个谓词中

时间:2013-09-01 21:53:22

标签: objective-c nspredicate nsfetchedresultscontroller predicate

我想设置一个带有下一个子预测的复合谓词:

  • id(AND type)
  • 名字(或类型)
  • 姓氏(或类型)
  • 中间名(或类型)

我正在阅读NSCompoundPredicate documentation但不明白 - 是否可以同时使用+ andPredicateWithSubpredicates:+ orPredicateWithSubpredicates:将它们组合为一个提取请求中的一个谓词?< / p>

1 个答案:

答案 0 :(得分:21)

解决这个问题:

objc:

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

夫特:

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