1

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

1 Answer 1

2

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")
                }}
        }
    })
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your response, this code actually works the issue now is i get this error. Any thoughts? In my question i did mention that what I am trying to save is a string into the array. [Error]: invalid type for key passengers, expected array, but got string (Code: 111, Version: 1.7.2)
object["passengers"] = userId should object?.addObject(userId!, forKey: "passengers") in order to save the string to the array in parse

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.