如何在Realm Swift中过滤为当前日期创建的事件? [英] How do I filter events created for the current date in the Realm swift?
问题描述
如何过滤Realm swift中为当前日期创建的事件? 我尝试了以下类似的操作,但这是错误的.
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
推荐答案
Create == NSDate()
形式的查询将检查确切的日期相等性,并将其与第二个进行比较.如果要检查日期是否在给定间隔之间,例如检查日期是否在特定日期,而不管一天中的什么时间,则可以执行BETWEEN
检查:
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
更新:
以下是完整的代码示例,可获取当天的所有日期模型:
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])
这篇关于如何在Realm Swift中过滤为当前日期创建的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!