Swift:如何从解析中获取今天生成的对象?

时间:2016-02-18 08:01:16

标签: swift datetime parse-platform

我是初学者,所以请在解释时保持友好,谢谢。

所以基本上我试图使用createdAt来获取今天制作的对象。

     let parseQuery = PFQuery(className: "request")

    parseQuery.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
        if let objects = objects {

            for object in objects {

                    self.reqDates.append(object.createdAt!)
            }
        }
    }

            let formatter = NSDateFormatter()
            formatter.dateFormat = "yyyy-MM-dd HH:mm"
            let formatteddate = formatter.stringFromDate(self.reqDates[indexPath.row])
            myCell.reqDate.text = formatteddate

当我执行此操作时,它会正确显示所有具有日期的对象。但是,我怎样才能显示今天的对象,而不是所有对象都被创建。 我希望我解释得足够好。

1 个答案:

答案 0 :(得分:0)

createdAt大于今天(午夜)的请求对象

 let parseQuery = PFQuery(className: "request")

            let cal = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
            let components = cal.components([.Day , .Month, .Year ], fromDate: NSDate())
            let todayDate = cal.dateFromComponents(components)

            parseQuery.whereKey("createdAt", greaterThan:todayDate!)

            parseQuery.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
                if let objects = objects {

                    for object in objects {
                        print(object.createdAt)
                    }
                }
            }