Search code examples
iosxcodeswiftparse-platformpfquery

save string to parse array in swift


I have a column in my parse table that i want to store names in when a user accepts another user, so it is one string at a time.

The column is called "passengers" and the string I am trying to store is an Id.

I successfully get the correct Id but saving it to the column in parse is my problem.

Here is my code

   var appendToPassengerArrayQuery = PFQuery(className: "Posts")
                        appendToPassengerArrayQuery.getObjectInBackgroundWithId(cell.postIdLabel.text!, block: {
                            (object:PFObject?, error: NSError?) -> Void in


                          // this is where I want to send the id to the array in the column of the correct post
                           object["passengers"] = userId
                            object?.saveInBackgroundWithBlock{
                                (success: Bool, error: NSError?)-> Void in
                            if (success) {
                                println("request deleted")

                            }
                            else {
                                println("cannot delete")
                                }}
                        })

The error I am getting is on the line object["passengers"] = userId

This is the error message cannot assign value type string to a value of type anyobject


Solution

  • The reason you are getting the error is because object: PFObject? is still unwrapped which means its value can be a nil or PFObject.

        var appendToPassengerArrayQuery = PFQuery(className: "Posts")
        appendToPassengerArrayQuery.getObjectInBackgroundWithId(cell.postIdLabel.text!, block: {
            (object:PFObject?, error: NSError?) -> Void in
            if object != nil {
                object!["passengers"] = userId
                object!.saveInBackgroundWithBlock{
                    (success: Bool, error: NSError?)-> Void in
                    if (success) {
                        println("request deleted")
                    }
                    else {
                        println("cannot delete")
                    }}
            }
        })