我想要NSPredicate给我返回结果的keyPath是nil…或者小于给定的日期。我在core data中使用nsprecate
这些工作正常…
let endDatePredicate = NSPredicate(format: "%K > %@", #keyPath(RepeatingTask.endDate), date as NSDate)
let endDatePredicate = NSPredicate(format: "%K == nil", #keyPath(RepeatingTask.endDate), date as NSDate)
但是我不能让这些与OR操作符正确工作…
let endDatePredicate = NSPredicate(format: "%K > %@ || %K == nil", #keyPath(RepeatingTask.endDate), date as NSDate)
let endDatePredicate = NSPredicate(format: "%K > %@ OR %K == nil", #keyPath(RepeatingTask.endDate), date as NSDate)
let endDatePredicate = NSPredicate(format: "(%K > %@) || (%K == nil)", #keyPath(RepeatingTask.endDate), date as NSDate)
你可以使用NSCompoundPredicate。例如:
let first = NSPredicate(format: "%K > %@", #keyPath(RepeatingTask.endDate), date as NSDate)
let second = NSPredicate(format: "%K == nil", #keyPath(RepeatingTask.endDate), date as NSDate)
let predicate = NSCompoundPredicate(orPredicateWithSubpredicates: [first, second])