Iphone 我可以对NSFetchRequest应用多个谓词吗?手动解析我的结果会更好吗?

Iphone 我可以对NSFetchRequest应用多个谓词吗?手动解析我的结果会更好吗?,iphone,core-data,nspredicate,nsfetchrequest,Iphone,Core Data,Nspredicate,Nsfetchrequest,好的,我有一个基本的iPad应用程序,要求用户提供5个搜索/筛选条件。基于这些数据,我需要转到我的核心数据数据库,并取出符合该标准的所有托管对象。似乎我需要对同一个请求应用多个谓词,这可能吗?或者我可以写一个很长的谓词吗?有多种需求?我应该怎么做 通过fetch请求抓取所有实体,然后在每个数组中循环并抓取任何符合搜索条件的对象,这是一个好主意吗 请告知 是的,这是可能的。您正在寻找复合谓词,下面是一个使用and谓词的示例: NSPredicate *compoundPredicate =

好的,我有一个基本的iPad应用程序,要求用户提供5个搜索/筛选条件。基于这些数据,我需要转到我的核心数据数据库,并取出符合该标准的所有托管对象。似乎我需要对同一个请求应用多个谓词,这可能吗?或者我可以写一个很长的谓词吗?有多种需求?我应该怎么做

通过fetch请求抓取所有实体,然后在每个数组中循环并抓取任何符合搜索条件的对象,这是一个好主意吗


请告知

是的,这是可能的。您正在寻找复合谓词,下面是一个使用and谓词的示例:

NSPredicate *compoundPredicate 
   = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray of Predicates]];
您还可以根据需要使用
notPredicateWithSubpredicates
或使用subpredicatewithsubpredicates

链接到文档

Swift 4
let fetchRequest:NSFetchRequest=YourModelEntityName.fetchRequest()
让fooValue=“foo”
让barValue=“bar”
让firstAttributePredicate=NSPredicate(格式:“firstAttribute=%@”,fooValue为CVarArg)
让secondAttributePredicate=NSPredicate(格式:“secondAttribute=%@”,barValue为CVarArg)
fetchRequest.predicate=NSCompoundPredicate(和PredicateWithSubpredicates:[firstAttributePredicate,secondAttributePredicate])

有关不同类型的
NSCompoundPredicate
构造函数的更多信息,请参见

Thanx man,我尝试编写一个NSString并将其作为格式传递给谓词。这很有效,但不适合约会!!我浪费了一天的工作。这是创建过滤器的方法:D
let fetchRequest: NSFetchRequest<YourModelEntityName> = YourModelEntityName.fetchRequest()

let fooValue = "foo"
let barValue = "bar"

let firstAttributePredicate = NSPredicate(format: "firstAttribute = %@", fooValue as CVarArg)
let secondAttributePredicate = NSPredicate(format: "secondAttribute = %@", barValue as CVarArg)

fetchRequest.predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [firstAttributePredicate, secondAttributePredicate])