【发布时间】:2017-09-14 10:57:49
【问题描述】:
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
}
}
}
}
【问题讨论】:
-
您是否告诉核心数据中的日期具有 hh:mm:ss 并且您希望在比较谓词时将其删除?
-
@SandeepBhandari 是的。在核心数据存储也与时间。当我在谓词中使用它来比较它时,我希望日期没有时间
-
@SandeepBhandari :) 谢谢!
-
对不起,我帮不上什么忙
-
好的.. 无论如何感谢您的尝试! :)
标签: ios swift core-data nspredicate