Getting data from findObjectsInBackgroundWithBlock

2019-02-15 05:48发布

问题:

I am using Parse to get data out of a database. When the block - findObjectsInBackgroundWithBlock is called an array gets passed. As I am only receiving one row of data, it all appears in one [0] section of the array. So how do i get all the bits out of that array ??

Here is some code I working with :

var MainPicture = PFQuery(className: "Staff")
        MainPicture.whereKey("Position", equalTo: "Sales Manager")
        MainPicture.findObjectsInBackgroundWithBlock ({(objects:[AnyObject]!, error: NSError!) in
            if(error == nil){
                for object in objects {

                }
                self.getMainImageData(objects as [PFObject])


            }
            else{
                println("Error in retrieving \(error)")
            }

        })

so where for object in objects is , it gives me one array with all the stuff in. So how can extract that array to get the First Name, Last Night, location, Staff ID out ?

thanks

回答1:

You'll need to cast your [AnyObject] to a [PFObject] and then you can use the standard Parse methods to get the data out.

if let staffObjects = objects as? [PFObject] {
  for staff in staffObjects {
    // Use staff as a standard PFObject now. e.g.
    let firstName = staff.objectForKey("first_name")
  }
}