Search code examples
iosswiftparse-platformpfquerycollectionview

Error with Parse: PFObject does not have a member named 'createdAt'


I am stuck on that issue. I've read the Parse documentation (https://parse.com/docs/ios/guide#queries-query-constraints) but it doesn't helps me that much.

The error

When I try to get "createdAt" or "updatedAt" from a PFObject, I got the following error:

['PFObject'] does not have a member named 'createdAt'

The Code

Here is the (shortened) function:

func loadCheckInData() {

    var query = PFQuery(className: "CheckIn")
    query.orderByDescending("createdAt")
    query.selectKeys(["firstName","lastName","updatedAt","createdAt"])

    query.findObjectsInBackgroundWithBlock({
        (objects: [AnyObject]?, error: NSError?) -> Void in

        if error == nil {

            println(objects)
            // Do something
            if let object = objects as? [PFObject] {
                println("\(object.createdAt)")                  <---- Error here
                self.CheckedPatients = Array(object.generate())
            }
        } else {
            // Log details of the failure
            println("Error: \(error!) \(error!.userInfo!)")
        } 
    }) 
}

Then I retrieve "firstName", "lastName" (and try to retrieve "createdAt") that are in my "CheckIn" Parse's class with the following code

func collectionView(cellView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    // Add parse data
    if let value = CheckedPatients[indexPath.row]["lastName"] as? String {
        cell.cellLabelTwo.text = value
    }
    if let value = CheckedPatients[indexPath.row]["firstName"] as? String {
        cell.cellLabel.text = value
    }
    if let value = CheckedPatients[indexPath.row]["createdAt"] as? String {
        cell.cellDay.text = value
    }
    return cell
}

And I call the function

override func viewDidAppear(animated: Bool) {
    loadCheckInData()
}

In fact I tried differents method to get that "createdAt" value, and I can't make it works. Does anyone have an idea (and a quick explanation if possible) it could be nice. Thank you !


Solution

  • I figured out how to get the property, thanks to @SanitLee answer. I post my code with the solution so that everybody could see it.

    Solution

    In my function named loadCheckInData() I added for object in objects { to the findObjectsInBackgroundWithBlock method. See below:

    func loadCheckInData() {
    
        var query = PFQuery(className: "CheckIn")
        query.orderByDescending("createdAt")
        query.findObjectsInBackgroundWithBlock({
            (objects: [AnyObject]?, error: NSError?) -> Void in
            if error == nil {
    
                // Do something
                if let objects = objects as? [PFObject] {
                    for object in objects {                 // <---  Added
                        // data stored in the var named "CheckedPatient"
                        self.CheckedPatients = Array(objects.generate())
                    }
                }
            } else {
                // ...
            }
        })
    }
    

    Then to retrieve the createdAt property inside the cellForItemAtIndexPath function, I changed:

    if let value = CheckedPatients[indexPath.row]["createdAt"] as? String {
        cell.cellDay.text = value
    }
    

    into:

        var DateVar : PFObject = CheckedPatients[indexPath.row]
        var dateFormatter:NSDateFormatter = NSDateFormatter()      // Formating
        dateFormatter.dateFormat = "EEEE dd MMM HH:mm"     
        cell.cellDay.text = dateFormatter.stringFromDate(DateVar.createdAt!)
    

    Now it works as I want. If you have any advice, thorough explanations or improvement about the code, feel free to modify and explain it.

    Thanks