Parse Cloud Code 中的调用函数

Calling function in Parse Cloud Code

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

       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 以将其解包。 但是,这样做会给我留下错误:

Cannot convert value of type '(AnyObject!, NSError!) -> Void' to expected argument type 'PFIdResultsBlock?'

我必须更改什么才能调用此函数?

尝试使用 PFObject? 而不是 AnyObject!

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 {

        }
    })
}