Search code examples
parse-platformparse-server

How to use Parse findObjectsInBackground to get objectID


I am saving / retrieving images from Parse server successfully, but when I try and get the objectID my code crashes. How should I get the objectId to use for future reference e.g deleting the object?

let query = PFQuery(className: "Images")

    query.findObjectsInBackground {
        (objects:[PFObject]?, error: Error?) -> Void in

        if error == nil && (objects != nil) {
            for object in objects! {

                let imageFile = object["imageFileObject"] as! PFFileObject
                let imageWidth = object["imageWidth"] as! CGFloat
                let imageName = object["imageName"] as! String
                let imageId = object["objectId"] as! String  // CRASHES ON THIS LINE
            }

Solution

  • You can retrieve the objectId like this:

    let imageId = object.objectId