我可以根据子 class 对象的属性创建谓词吗?
Can I create a predicate based on the properties of child class objects?
我有两个 classes:Activity 和 Action。 Activity是parent class,Action是child,这是一对多的关系。在设置 NSFetchedResultsController 时,我想设置一个基于子 class 属性的谓词。这是一个例子...
fetchRequest.predicate = NSPredicate(format: "filter only activities that have actions which have their date property equalling today")
如果我要使用 for in 循环过滤活动,这就是它的样子...
for activity in activities
{
if activity.actions != nil
{
for action in activity.actions
{
if action.date == today
{
// add activity to filtered array
}
}
}
}
您可以在 NSPredicate
中使用 SUBQUERY
。这里我用了一个NSArray
作为示范。
let today = Date()
let array: NSArray = [
Activity(actions: [
Action(date: today)
]),
Activity(actions: [
Action(date: Date(timeIntervalSince1970: 0))
])
]
// here is your predicate
let result = array.filtered(using: NSPredicate(format: "SUBQUERY(actions, $action, $action.date == %@) .@count > 0", today as CVarArg))
print(result.count) // 1
请注意,我在这里使用 ==
来比较日期,这意味着只有日期 正好 等于 today
时才会匹配。如果 "today" 你的意思是 "any time in the 24 hours",那么你将不得不使用 this question:
中建议的答案
func getLastAndNextMidnight(date: Date) -> (Date, Date) {
let dateComponents = Calendar.current.dateComponents([.year, .month, .day], from: date)
var oneDay = DateComponents()
oneDay.day = 1
let lastMidnight = Calendar.current.date(from: dateComponents)!
let nextMidnight = Calendar.current.date(byAdding: oneDay, to: lastMidnight)!
return (lastMidnight, nextMidnight)
}
let midnights = getLastAndNextMidnight(date: today)
let result = array.filtered(using: NSPredicate(format: "SUBQUERY(actions, $action, ($action.date >= %@) AND ($action.date <= %@)) .@count > 0", midnights.0 as CVarArg, midnights.1 as CVarArg))
print(result.count)
今天有什么活动吗?谓词格式:
"ANY actions.date == today"
我有两个 classes:Activity 和 Action。 Activity是parent class,Action是child,这是一对多的关系。在设置 NSFetchedResultsController 时,我想设置一个基于子 class 属性的谓词。这是一个例子...
fetchRequest.predicate = NSPredicate(format: "filter only activities that have actions which have their date property equalling today")
如果我要使用 for in 循环过滤活动,这就是它的样子...
for activity in activities
{
if activity.actions != nil
{
for action in activity.actions
{
if action.date == today
{
// add activity to filtered array
}
}
}
}
您可以在 NSPredicate
中使用 SUBQUERY
。这里我用了一个NSArray
作为示范。
let today = Date()
let array: NSArray = [
Activity(actions: [
Action(date: today)
]),
Activity(actions: [
Action(date: Date(timeIntervalSince1970: 0))
])
]
// here is your predicate
let result = array.filtered(using: NSPredicate(format: "SUBQUERY(actions, $action, $action.date == %@) .@count > 0", today as CVarArg))
print(result.count) // 1
请注意,我在这里使用 ==
来比较日期,这意味着只有日期 正好 等于 today
时才会匹配。如果 "today" 你的意思是 "any time in the 24 hours",那么你将不得不使用 this question:
func getLastAndNextMidnight(date: Date) -> (Date, Date) {
let dateComponents = Calendar.current.dateComponents([.year, .month, .day], from: date)
var oneDay = DateComponents()
oneDay.day = 1
let lastMidnight = Calendar.current.date(from: dateComponents)!
let nextMidnight = Calendar.current.date(byAdding: oneDay, to: lastMidnight)!
return (lastMidnight, nextMidnight)
}
let midnights = getLastAndNextMidnight(date: today)
let result = array.filtered(using: NSPredicate(format: "SUBQUERY(actions, $action, ($action.date >= %@) AND ($action.date <= %@)) .@count > 0", midnights.0 as CVarArg, midnights.1 as CVarArg))
print(result.count)
今天有什么活动吗?谓词格式:
"ANY actions.date == today"