保存字符串以在 swift 中解析数组

save string to parse array in swift

我的解析 table 中有一个列,我想在一个用户接受另一个用户时存储名称,所以它一次是一个字符串。

该列名为 "passengers",我要存储的字符串是一个 ID。

我成功获得了正确的 Id,但将其保存到解析列中是我的问题。

这是我的代码

   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")
                                }}
                        })

我得到的错误是在线 object["passengers"] = userId

这是错误信息 cannot assign value type string to a value of type anyobject

您收到错误的原因是 object: PFObject? 仍未包装,这意味着它的值可以是 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")
                }}
        }
    })