H. 나는 Agendadate라는 엔티티와 AgendaEvent라는 엔티티가 있습니다. AgendaEvent에는 AgendaDate (agendaDates)와의 많은 관계가 있습니다.Swift coreData - 형식 날짜와 술어에서 사용
내 AgendaDate에 개체 날짜 (날짜 유형)가 있습니다.
내가 지금 같은 술어를 사용하고 있습니다 : YYYY MM은 HH dd는
"DD MM 일"대신 "의 :
fetchRequest.predicate = NSPredicate(format: "ANY agendaDates.dates == %@", date as CVarArg)
내가 형식으로 시도하고이를 하시려면 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