6

我想用下一个子谓词设置一个复合谓词:

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

我正在阅读NSCompoundPredicate 文档,但理解不够清楚 - 是否有可能同时使用两者+ andPredicateWithSubpredicates:并将+ orPredicateWithSubpredicates:它们作为一个谓词组合在一个提取请求中?

4

1 回答 1

22

以这种方式解决:

对象:

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])
于 2013-09-01T22:35:17.120 回答