Я получаю этот необязательный (SUBQUERY (повестки дня, $ a, $ a.dates> = CAST (526860000.000000, "NSDate") И $ a.dates <CAST (526946400.000000, "NSDate")). @ Count> 0)
меня есть объект с именем Agendadate и один с именем AgendaEvent. AgendaEvent имеет отношение многие ко многим с AgendaDate (повестки дня).
в моей AgendaDate у меня есть объект даты (типа Дата).
Я использую предикат так:
fetchRequest.predicate = NSPredicate(format: "ANY agendaDates.dates == %@", date as CVarArg)
Я пытаюсь отформатировать даты, чтобы иметь это:
"дд ММ гггг" вместо "гггг ММ дд чч: мм: сс"
Мне нужно сравнить в предикате две даты, но без времени? Является ли это возможным?
ОБНОВЛЕНИЕ ----- Вот моя функция обновлена, как вы предложили:
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
}
}
}
}