我第一次使用云代码,并尝试调用以下函数:
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!) - >无效的 期望参数类型'PFIdResultsBlock?'
为了调用此功能,我必须更改什么?
答案 0 :(得分:0)
尝试使用PFObject?
代替AnyObject!
。
答案 1 :(得分:0)
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 {
}
})
}