Swift:解析,基于nsdate()查询日期字段 [英] Swift : Parse, query date field based on nsdate()
问题描述
我正在尝试检索今天添加的要分析的记录,但是查询不返回任何结果.我如何获取查询以根据今天的日期返回结果.
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
推荐答案
您的问题是NSDate
不仅是日期,而是确切的时间.
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)
上述解决方案仅适用于iOS 8和更高版本;较新(,我在这里找到了它).如果您想获得与iOS 7兼容的内容,请单击该链接.
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.
这篇关于Swift:解析,基于nsdate()查询日期字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!