I'm having some struggles with the code when trying to retrieve an PFObject from Parse.
This is my code:
var query = PFQuery(className: "message")
query.whereKey("recipientUsername", equalTo: PFUser.currentUser())
var messages = query.findObjects()
var done = false
for message in messages {
if done == false {
var messageObject:PFObject =
done == true
The problem is in the "var messageObject:PFObject = ". I do not know what to write to complete this statement.
Any ideas on how to proceed would be appreciated.
Whatever you have to do with the messageObject
variable, you don't need it. The findObjects
method returns an array of PFObjects
. Since I presume that under the hood it returns NSArray
, which is translated to [AnyObject]
in swift, you just have to downcast to an array of PFObject
:
var messages = query.findObjects() as [PFObject]
then in your loop the message
variable is automatically inferred the PFObject
type, hence you don't need to create another messageObject
variable:
for message in messages { // message is of PFObject type
if done == false {
// Do whatever you need with message
println(message)
}
}
Update Sep 14, 2015: as of Swift 1.2, the new as!
forced conversion operator must be used:
var messages = query.findObjects() as! [PFObject]
Thanks to @Kiran Ruth R
for pointing that out.