Abh*_*ale 4 core-data ios swift
我对 iOS 开发相当陌生,所以这可能是显而易见的。
使用核心数据,我有一个实体: 'Post' ,其属性为“dateAdded”。
当用户在日历上选择 NSDate 时,我只想获取当天的所有条目并将其加载到表中。
我正在考虑使用一个谓词,其中 dateAdded (NSDate) 等于 calendarSelectedNSDate。但我认为这不会起作用,因为他们的 NSDate 会因为不同的时间而不同。
我真的很感激一个解决方案!谢谢你!
用于(NS)Calendar计算所选日期的开始和结束并创建谓词:
// get the current calendar
let calendar = Calendar.current
// get the start of the day of the selected date
let startDate = calendar.startOfDay(for: calendarSelectedNSDate)
// get the start of the day after the selected date
let endDate = calendar.date(byAdding: .day, value: 1, to: startDate, options: .matchNextTime)!
// create a predicate to filter between start date and end date
let predicate = NSPredicate(format: "dateAdded >= %@ AND dateAdded < %@", startDate as NSDate, endDate as NSDate)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3588 次 |
| 最近记录: |