行模板可以将谓词转换为predicate(withSubpredicates:)
,无需其他覆盖。在 IB 中,正确的表达式是日期。
override func predicate(withSubpredicates subpredicates: [NSPredicate]?) -> NSPredicate {
// call super to get the predicate, for example aDate == '3.5.20210 14:03:53'
let predicate = super.predicate(withSubpredicates: subpredicates)
// convert the predicate to aDate >= '3.5.2021 00:00:00' AND aDate < '4.5.2021 00:00:00'
var newPredicate = predicate
if let comparisonPredicate = predicate as? NSComparisonPredicate,
let predicateDate = comparisonPredicate.rightExpression.constantValue as? Date {
let keyPath = comparisonPredicate.leftExpression.keyPath
var components = Calendar.current.dateComponents([.year, .month, .day], from: predicateDate)
components.hour = 0
components.minute = 0
components.second = 0
components.calendar = NSCalendar.current
switch comparisonPredicate.predicateOperatorType {
case .lessThan:
// aDate < '3.5.2021 00:00:00'
let date = components.date! as NSDate
newPredicate = NSPredicate(format: "%K < %@", keyPath,date)
case .lessThanOrEqualTo:
// aDate < '4.5.2021 00:00:00'
components.day = components.day! + 1
let date = components.date! as NSDate
newPredicate = NSPredicate(format: "%K < %@", keyPath,date)
case .greaterThan:
// aDate >= '4.5.2021 00:00:00'
components.day = components.day! + 1
let date = components.date! as NSDate
newPredicate = NSPredicate(format: "%K >= %@", keyPath,date)
case .greaterThanOrEqualTo:
// aDate >= '3.5.2021 00:00:00'
let date = components.date! as NSDate
newPredicate = NSPredicate(format: "%K >= %@", keyPath,date)
case .equalTo:
// aDate >= '3.5.2021 00:00:00' AND aDate < '4.5.2021 00:00:00'
let startDate = components.date! as NSDate
components.day = components.day! + 1
let endDate = components.date! as NSDate
newPredicate = NSPredicate(format: "%K >= %@ AND %K < %@", keyPath, startDate, keyPath, endDate)
case .notEqualTo:
// NOT (aDate >= '3.5.2021 00:00:00' AND aDate < '4.5.2021 00:00:00')
let startDate = components.date! as NSDate
components.day = components.day! + 1
let endDate = components.date! as NSDate
newPredicate = NSPredicate(format: "NOT (%K >= %@ AND %K < %@)", keyPath, startDate, keyPath, endDate)
default:
newPredicate = predicate
}
}
return newPredicate
}