繁体   English   中英

Swift Core Data从特定日期获取结果

[英]Swift Core Data Fetch results from a certain date

我对ioS dev相当新,所以这可能是显而易见的。

使用Core Data,我有一个实体:'Post',其属性为“dateAdded”。

当用户在日历上选择NSDate时 - 我想仅获取当天的所有条目并将其加载到表中。

我在考虑使用谓词,其中dateAdded(NSDate)将等于calendarSelectedNSDate。 但我认为这不会起作用,因为他们的NSD由于时间的不同会有所不同。

我真的很感激解决方案! 谢谢!

使用NSCalendar计算所选日期的开始和结束并创建谓词:

// get the current calendar
let calendar = NSCalendar.currentCalendar()
// get the start of the day of the selected date
let startDate = calendar.startOfDayForDate(calendarSelectedNSDate)
// get the start of the day after the selected date
let endDate = calendar.dateByAddingUnit(.Day, value: 1, toDate: startDate, options: NSCalendarOptions())!
// create a predicate to filter between start date and end date
let predicate = NSPredicate(format: "dateAdded >= %@ AND dateAdded < %@", startDate, endDate)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM