我正在尝试创建一个UITableView
,其中填充了所选日期的对象(具体为yesterday
,today
和tomorrow
)。在我的CoreData模型中,有问题的实体作为属性date with type
Date`。
要获得yesterday
,today
和tomorrow
我这样做(按照this WWDC session的规定):
var today = Date()
var yesterday = calendar.date(byAdding: .day, value: -1, to: self.date)
var tomorrow = calendar.date(byAdding: .day, value: 1, to: self.date)
并执行:
today = calendar.startOfDay(for: today)
yesterday = calendar.startOfDay(for: yesterday)
tomorrow = calendar.startOfDay(for: tomorrow)
一旦我得到所有日期,我不知道该怎么做。目前我的获取结果控制器看起来像:
lazy var fetchedResultsController: NSFetchedResultsController<Entry> = {
let fetchRequest: NSFetchRequest<Entry> = Entry.fetchRequest()
// Configure Fetch Request
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
// Create Fetched Results Controller
let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: app_delegate.persistentContainer.viewContext, sectionNameKeyPath: nil, cacheName: nil)
// Configure Fetched Results Controller
fetchedResultsController.delegate = self
return fetchedResultsController
}()
如何将fetchedResultsController
限制为其中一个日期,如果可能,我会使用Calendar
方法执行此操作:isDateInToday()
?
答案 0 :(得分:0)
为获取请求添加谓词。
request.predicate = NSPredicate(format: "date > %@ && date < %@",
today as NSDate, tomorrow as NSDate)