How do I filter events created for the current date in the Realm swift?

≯℡__Kan透↙ 提交于 2019-12-20 23:48:12

问题


How do I filter events created for the current date in the Realm swift? I tried something like below but this wrong.

let dtSource = datasource.filter("Create == NSDate()").count

Update: Getting the filter creating my date as a string.

http://i.stack.imgur.com/8fLX9.png

http://i.stack.imgur.com/HDR2X.png


回答1:


A query in the form of Create == NSDate() will check exact date equality, which will compare down to the second. If you want to check if a date is between a given interval, like checking if it's on a specific day, regardless of the time of day, you could do a BETWEEN check:

let dtSource = datasource.filter("Create BETWEEN %@", [firstDate, secondDate]).count

Update:

Here's a full code sample to get all date models for the current day:

import RealmSwift

class Event: Object {
    dynamic var date = NSDate()
}

let todayStart = Calendar.current.startOfDay(for: Date())
let todayEnd: Date = {
  let components = DateComponents(day: 1, second: -1)
  return Calendar.current.date(byAdding: components, to: todayStart)!
}()
events = realm.objects(Event.self).filter("date BETWEEN %@", [todayStart, todayEnd])


来源:https://stackoverflow.com/questions/35964884/how-do-i-filter-events-created-for-the-current-date-in-the-realm-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!