Search code examples
arraysswiftparse-platformpfquery

How do you display the latest single entry from row and single Column using Swift Code


I am able to get to this point, and not sure how to print the result of the latest entry. I've a class called "Transaction" and multiple column, and hundreds of rows that is entered in different date and time. I'm only interested to display the latest entry from the class. Please help if you have any idea. thanks.

let query = PFQuery(className: "Transaction")
query.whereKey("city", equalTo: PFUser.currentUser()!.username!)
query.orderByDescending("createdAt")
query.limit = 1
query.findObjectsInBackgroundWithBlock {(objects:[PFObject]?, error:NSError?) -> Void in
if error == nil{
   print ("Well Done!)
}
else{
  print("too bad, something went wrong!")
}

}
}

Solution

  • To get access to the objects that are returned, you can do this:

    if error == nil {
        let foundObjects: [PFObject] = objects!
        for object in foundObjects {
            print(object["city"])
        }
    }