H. 我有一个名为Agendadate的实体,一个名为AgendaEvent。 AgendaEvent与AgendaDate(议程日期)具有多对多关系。Swift coreData - 格式日期并在谓词中使用它
在我的AgendaDate我有一个对象日期(类型日期)。
我使用的是谓语,像这样:
fetchRequest.predicate = NSPredicate(format: "ANY agendaDates.dates == %@", date as CVarArg)
我试图格式化日期有这样的:
“年月日”,而不是“年月日HH:MM: ss“
我需要比较谓词中的两个日期,但没有时间? 有可能吗?
UPDATE ----- 这里是我的功能更新,你建议:
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 < %@)[email protected] > 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
}
}
}
}
你告诉核心数据中的日期有hh:mm:ss,你希望它在删除谓词时比较吗? –
@SandeepBhandari是的。核心数据也随时间存储。当我在谓词中使用它来比较它时,我希望日期没有时间 – Marco
@SandeepBhandari :)谢谢! – Marco