Swift : Parse, query date field based on nsdate()

烂漫一生 提交于 2020-01-14 14:31:48

问题


i am trying to retrieve records which were added today to parse, but the query does not return any results. how can i get the query to return the results based on todays date.

let now = NSDate()
var query = PFQuery(className:"userBids")
    query.whereKey("date", equalTo: now)

the parse date field is set to date. please help


回答1:


Your problem is that NSDate is not just a date, it's an exact moment in time.

And you're very likely to have exactly no records from this precise date and moment in time.

What you should be doing is something like:

let now = NSDate()
let cal = NSCalendar(calendarIdentifier: NSGregorianCalendar)
let midnightOfToday = cal.startOfDayForDate(now)
var query = PFQuery(className:"userBids")
    query.whereKey("date", greaterThanOrEqualTo: midnightOfToday)

The above solution only works for iOS 8 & newer (and I found it here). Click on that link if you want to get something that's iOS 7 compatible.



来源:https://stackoverflow.com/questions/31379063/swift-parse-query-date-field-based-on-nsdate

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