swift - Error with realm query using NSDate() -
i have code in swift:
lists = sharedappcore.getrealm().objects(event).filter("status = 1 or status = 2").sorted("end_date", ascending: false)
now want filter start_date nsdate() not work:
lists = sharedappcore.getrealm().objects(event).filter("status = 1 or status = 2 or start_date >= \(nsdate())").sorted("end_date", ascending: false)
any ideas?
lists = sharedappcore.getrealm() .objects(event) .filter("status = 1 or status = 2 or start_date >= \(nsdate())") .sorted("end_date", ascending: false)
strictly speaking, above code not same final code.
filter("status = 1 or status = 2").filter(predicate).sorted("end_date", ascending: false)
^ because predicate same following:
filter("(status = 1 or status = 2) , end_date >= %@", nsdate())
if create predicate or
, can following:
filter("status = 1 or status = 2 or end_date >= %@", nsdate())
additionally, if compare without hours, should truncate hours date first. compare truncated date.
like following:
let = nsdate() let calendar = nscalendar.currentcalendar() let component = calendar.components([.year, .month, .day], fromdate: now) let today = calendar.datefromcomponents(component)! // truncated time
then use truncated date compare in predicate.
let = nsdate() let calendar = nscalendar.currentcalendar() let component = calendar.components([.year, .month, .day], fromdate: now) let today = calendar.datefromcomponents(component)! // truncated time let lists = realm .objects(event) .filter("status = 1 or status = 2 or end_date >= %@", today) .sorted("end_date", ascending: false)
Comments
Post a Comment