在Swift中向PFObject添加用户指针

时间:2015-04-20 18:49:43

标签: swift parse-platform pfobject pfuser

我在Swift中使用Parse SDK并尝试创建一个新的投票对象,然后保存指向当前用户的指针。

我收到以下错误 - [错误]:无法添加关系的非指针(代码:111,版本:1.7.1)。

func createVoteObject(row: Int, quest: PFObject) {
    var vote = PFObject(className: "Votes")
    vote["user"] = PFUser.currentUser()

    vote.saveInBackgroundWithBlock {
        (success: Bool, error: NSError?) -> Void in
        if error != nil {
            println("Error saving vote object: \(error!)")
        } else {
            Do something else
        }
    }
}

添加其他属性我没有问题。

我对此功能遇到同样的问题

func saveVoteToUserObject(vote: PFObject, quest: PFObject) {

    if let currentUser = PFUser.currentUser() {
        var userToVoteRelation = currentUser.relationForKey("votes")
        userToVoteRelation.addObject(vote)
        var userToQuestRelation = currentUser.relationForKey("votedOnQuests")
        userToQuestRelation.addObject(quest)

        currentUser.saveInBackgroundWithBlock {
            (success: Bool, error: NSError?) -> Void in
            if error != nil {
                println(error)
            } else {
                println("Successfully saved vote to User object")
            }
        }
    }
}

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

Wain激发了对此的修复。必须通过清除缓存并重新登录来刷新用户(使用PFUser.logOut()或在iOS模拟器中重置内容和设置)。

每次尝试创建指针时,都需要使用最新版本的指针对象。我在User对象中添加了一些字段,这些字段在我的PFUser.currentUser()实例中没有更新,因为它是缓存的。

答案 1 :(得分:0)

确认。我使用

看到了这个错误
newProfileImage["owner"] = PFUser.currentUser()

设置指向用户的指针。以下作品:

PFUser.currentUser()?.fetch()

newProfileImage.setObject(PFUser.currentUser()!, forKey: "owner")