【问题标题】:Calling function in Parse Cloud CodeParse Cloud Code 中的调用函数
【发布时间】:2016-04-14 01:32:04
【问题描述】:

我是第一次使用云代码并尝试调用以下函数:

       let friendRequest: PFObject = self.friendRequestsToCurrentUser[sender.tag] as PFObject
      let fromUser: PFUser = friendRequest[FriendRequestKeyFrom] as PFUser
      //call the cloud code function that adds the current user to the user who sent the request and pass in the friendRequest id as a parameter
        PFCloud.callFunctionInBackground("addFriendToFriendsRelation", withParameters: ["friendRequest": friendRequest.objectId]) { (object:AnyObject!, error: NSError!) -> Void in
        let friendsRelation: PFRelation = PFUser.currentUser()!.relationForKey("friends")
         friendsRelation.addObject(fromUser)
             self.currentUser.saveInBackgroundWithBlock({ (succeeded: Bool, error: NSError!) -> Void in
                 if succeeded {
                 } else {
            }
        })
    }
}

实现该功能后,我需要添加“!”到参数中的 objectId 以展开它。 但是,这样做会给我留下错误:

无法将 '(AnyObject!, NSError!) -> Void' 类型的值转换为 预期参数类型“PFIdResultsBlock?”

我必须改变什么才能调用这个函数?

【问题讨论】:

    标签: ios swift parse-platform parse-cloud-code


    【解决方案1】:

    PFIdResultsBlock 对应于以下签名(AnyObject?, NSError?) -> Void,因此请尝试将您的代码更改为:

    let friendRequest: PFObject = self.friendRequestsToCurrentUser[sender.tag] as PFObject
    let fromUser: PFUser = friendRequest[FriendRequestKeyFrom] as PFUser
    
    //call the cloud code function that adds the current user to the user who sent the request and pass in the friendRequest id as a parameter
    PFCloud.callFunctionInBackground("addFriendToFriendsRelation", withParameters: ["friendRequest": friendRequest.objectId]) { (object:AnyObject?, error: NSError?) -> Void in
        let friendsRelation: PFRelation = PFUser.currentUser()!.relationForKey("friends")
        friendsRelation.addObject(fromUser)
        self.currentUser.saveInBackgroundWithBlock({ (succeeded: Bool, error: NSError!) -> Void in
            if succeeded {
    
            } else {
    
            }
        })
    }
    

    【讨论】:

    • 感谢您的帮助!
    【解决方案2】:

    尝试使用PFObject? 而不是AnyObject!

    【讨论】:

    • 我尝试了 PFObject 但仍然收到错误消息。谢谢你的建议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-25
    • 1970-01-01
    • 2016-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多