Swift coreData - 格式化日期并在谓词中使用它 [英] Swift coreData - format date and use it in predicate
问题描述
小时。
我有一个名为Agendadate的实体和一个名为AgendaEvent的实体。
AgendaEvent与AgendaDate(agendaDates)有很多关系。
H. I have an entity called Agendadate and one called AgendaEvent. AgendaEvent has a many to many relation with AgendaDate (agendaDates).
在我的AgendaDate中我有一个对象日期(类型为Date)。
in my AgendaDate i have an object dates (of type Date).
我正在使用这样的谓词:
I'm using a predicate like so:
fetchRequest.predicate = NSPredicate(format: "ANY agendaDates.dates == %@", date as CVarArg)
我正在尝试设置日期格式:
I'm trying to format dates to have this:
dd MM yyyy而不是yyyy MM dd hh:mm:ss
"dd MM yyyy" instead of "yyyy MM dd hh:mm:ss"
我需要在谓词中比较两个日期但没有时间吗?
有可能吗?
I need to compare in the predicate two dates but without the time? is it possible?
UPDATE -----
这是我的功能更新,如你所知:
UPDATE----- Here's my function updated as you have suggested:
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
}
这里是应该配置单元格的函数,只接受具有相同日期的事件:
and here's the function that should configure the cell and take just the events which have the same date of the selected date:
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
}
}
}
}
推荐答案
不要为此目的使用自定义字符串格式。您想要获取
与Agendadate对象相关的所有条目,并在给定日期的同一天使用日期
。
Don't use a custom string format for that purpose. You want to fetch all entries which are related to an Agendadate object with a date on the same day as the given day.
所以你首先计算当天的开始和结束:
So you compute the start and end of that day first:
let date = Date()
let cal = Calendar.current
let startOfDay = cal.startOfDay(for: date)
let endOfDay = cal.date(byAdding: .day, value: 1, to: startOfDay)!
然后使用此SUBQUERY:
Then use this 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。
It tests $a.dates >= startDate AND $a.dates < endDate
for all
related objects, and yields true if there is at least one matching
the condition.
这篇关于Swift coreData - 格式化日期并在谓词中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!