Getting parse.com data in Swift
I was able to successfully save Parse via Swift, but I'm having problems getting the data (and all the fetching tutorials seem to be for Obj-C).
Here's my code (with id edited).
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 entries in this class right now, but if I want to pull data for all of them, how do I get an object using an ID, if I'm not sure what the IDs are? I am guessing that I can access them sequentially as an array, but I am not quite sure how to do this, and I am confused as the only command I know to get it seems to require knowing the ID.
Thanks for any help!
source to share
The official parsing documentation explains how to make queries - there is sample code in swift.
In your case, you should 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 the closure with an array of objects matching the request - since there is no filter in the request, it just returns all records.
source to share