问题
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