簡體   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