Swift coreData - 格式化日期并在谓词中使用它
Swift coreData - format date and use it in predicate
H.
我有一个名为 Agendadate 的实体和一个名为 AgendaEvent 的实体。
AgendaEvent 与 AgendaDate (agendaDates) 具有多对多关系。
在我的 AgendaDate 中我有一个对象日期(日期类型)。
我正在使用这样的谓词:
fetchRequest.predicate = NSPredicate(format: "ANY agendaDates.dates == %@", date as CVarArg)
我正在尝试格式化日期以使其具有以下内容:
"dd MM yyyy" 而不是 "yyyy MM dd hh:mm:ss"
我需要在谓词中比较两个日期但没有时间?
可能吗?
更新-----
这是我按照您的建议更新的功能:
func agendaEventsWithDate(date: Date) -> NSFetchRequest<AgendaEvent>
{
// create a fetch request that will retrieve all the AgendaEvents.
let fetchRequest = NSFetchRequest<AgendaEvent>(entityName: "AgendaEvent")
// set the predicate to only keep AgendaEvents where the related AgendaDate's date matches the passed in date.
let cal = Calendar.current
let startOfDay = cal.startOfDay(for: eventDate)
let endOfDay = cal.date(byAdding: .day, value: 1, to: startOfDay)!
print(startOfDay)
print(endOfDay)
// fetchRequest.predicate = NSPredicate(format: "ANY agendaDates.agendaDates == %@", date as CVarArg)
fetchRequest.predicate = NSPredicate(format: "SUBQUERY(agendaDates, $a, $a.dates >= %@ AND $a.dates < %@).@count > 0",
startOfDay as NSDate, endOfDay as NSDate)
return fetchRequest
}
这里是应该配置单元格并只获取与所选日期具有相同日期的事件的函数:
func configuringCell(cell: CalendarAgendaCell, indexPath: IndexPath) {
for dates in calendar.selectedDates {
for dateOfEvent in myEventDate {
formatter.dateFormat = "dd MM yyyy"
let dateToCompare = formatter.string(from: dates)
formatter.dateFormat = "dd-MM-yyyy"
let comparingDate = formatter.date(from: dateToCompare)!
if dateOfEvent == dateToCompare {
myTempEvents = try! context.fetch(agendaEventsWithDate(date: comparingDate))
let myEvent = myTempEvents[indexPath.row]
cell.configureCell(agendaEvent: myEvent)
} else {
// the array is empty
}
}
}
}
不要为此目的使用自定义字符串格式。你想取
所有与具有日期的议程对象相关的条目
在给定日期的同一天。
所以你先计算那一天的开始和结束:
let date = Date()
let cal = Calendar.current
let startOfDay = cal.startOfDay(for: date)
let endOfDay = cal.date(byAdding: .day, value: 1, to: startOfDay)!
然后使用这个 SUBQUERY:
let predicate = NSPredicate(format: "SUBQUERY(agendaDates, $a, $a.dates >= %@ AND $a.dates < %@).@count > 0",
startOfDay as NSDate, endOfDay as NSDate)
它测试 $a.dates >= startDate AND $a.dates < endDate
所有人
相关对象,如果至少有一个匹配则返回 true
条件。
H. 我有一个名为 Agendadate 的实体和一个名为 AgendaEvent 的实体。 AgendaEvent 与 AgendaDate (agendaDates) 具有多对多关系。
在我的 AgendaDate 中我有一个对象日期(日期类型)。
我正在使用这样的谓词:
fetchRequest.predicate = NSPredicate(format: "ANY agendaDates.dates == %@", date as CVarArg)
我正在尝试格式化日期以使其具有以下内容:
"dd MM yyyy" 而不是 "yyyy MM dd hh:mm:ss"
我需要在谓词中比较两个日期但没有时间? 可能吗?
更新----- 这是我按照您的建议更新的功能:
func agendaEventsWithDate(date: Date) -> NSFetchRequest<AgendaEvent>
{
// create a fetch request that will retrieve all the AgendaEvents.
let fetchRequest = NSFetchRequest<AgendaEvent>(entityName: "AgendaEvent")
// set the predicate to only keep AgendaEvents where the related AgendaDate's date matches the passed in date.
let cal = Calendar.current
let startOfDay = cal.startOfDay(for: eventDate)
let endOfDay = cal.date(byAdding: .day, value: 1, to: startOfDay)!
print(startOfDay)
print(endOfDay)
// fetchRequest.predicate = NSPredicate(format: "ANY agendaDates.agendaDates == %@", date as CVarArg)
fetchRequest.predicate = NSPredicate(format: "SUBQUERY(agendaDates, $a, $a.dates >= %@ AND $a.dates < %@).@count > 0",
startOfDay as NSDate, endOfDay as NSDate)
return fetchRequest
}
这里是应该配置单元格并只获取与所选日期具有相同日期的事件的函数:
func configuringCell(cell: CalendarAgendaCell, indexPath: IndexPath) {
for dates in calendar.selectedDates {
for dateOfEvent in myEventDate {
formatter.dateFormat = "dd MM yyyy"
let dateToCompare = formatter.string(from: dates)
formatter.dateFormat = "dd-MM-yyyy"
let comparingDate = formatter.date(from: dateToCompare)!
if dateOfEvent == dateToCompare {
myTempEvents = try! context.fetch(agendaEventsWithDate(date: comparingDate))
let myEvent = myTempEvents[indexPath.row]
cell.configureCell(agendaEvent: myEvent)
} else {
// the array is empty
}
}
}
}
不要为此目的使用自定义字符串格式。你想取 所有与具有日期的议程对象相关的条目 在给定日期的同一天。
所以你先计算那一天的开始和结束:
let date = Date()
let cal = Calendar.current
let startOfDay = cal.startOfDay(for: date)
let endOfDay = cal.date(byAdding: .day, value: 1, to: startOfDay)!
然后使用这个 SUBQUERY:
let predicate = NSPredicate(format: "SUBQUERY(agendaDates, $a, $a.dates >= %@ AND $a.dates < %@).@count > 0",
startOfDay as NSDate, endOfDay as NSDate)
它测试 $a.dates >= startDate AND $a.dates < endDate
所有人
相关对象,如果至少有一个匹配则返回 true
条件。