Search code examples
swiftparse-platform

Retrieving parse.com data in Swift


I've been able to save successfully to Parse via Swift, but am having trouble retrieving data (and all of the tutorials on retrieving seem to be for Obj-C).

Here's my code (with Id's redacted).

Parse.setApplicationId("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", clientKey: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")


    var query = PFQuery(className: "EventObject")
    query.getObjectInBackgroundWithId(objectId: String!) {
        (event: PFObject!, error: NSError!) -> Void in

        if error == nil {
            println(event)

        } else {

        println(error)

        }

    }

I have 4 records in this class right now, but if I want to pull the data for all of them, how do I get the Object using the ID if I'm not sure what the IDs are? I'm assuming I can access them sequentially as an array, but I'm not quite clear how to do that, and am confused, as the only command I know to retrieve appears to require knowing the ID.

Thanks for any help!


Solution

  • The official parse documentation explains how to make queries - there is sample code in swift.

    In your case you have to use findObjectsInBackgroundWithBlock:

    var query = PFQuery(className:"EventObject")
    query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]!, error: NSError!) -> Void in
      if error == nil {
        for object in objects {
            // Do something
        }
      } else {
          println(error)
      }
    }
    

    which, if successful, provides to the closure an array of objects matching the query - since there's no filter set in the query, it just returns all records.