简体   繁体   English

解析云代码中的调用功能

[英]Calling function in Parse Cloud Code

I am working with cloud code for the first time and am trying to call the following function: 我是第一次使用云代码,正在尝试调用以下函数:

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

After implementing the function I was required to add "!" 实现该功能后,我需要添加“!” to the objectId in the parameter to unwrap it. 到参数中的objectId进行解包。 However, doing this leaves me with the error: 但是,这样做会给我带来错误:

Cannot convert value of type '(AnyObject!, NSError!) -> Void' to expected argument type 'PFIdResultsBlock?' 无法将类型'((AnyObject !, NSError!)-> Void'的值转换为预期的参数类型'PFIdResultsBlock?'

What must I change in order to call this function? 我必须更改什么才能调用该功能?

Try using PFObject? 尝试使用PFObject? instead of AnyObject! 而不是AnyObject! .

The PFIdResultsBlock corresponds to the following signature (AnyObject?, NSError?) -> Void so try to change your code to this: 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 {

        }
    })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM