Search code examples
iosswiftparse-platformpubnub

Cannot convert value of type "PFUser?" to type "[NSObject : AnyObject]


I'm not sure how to convert a PFUser to [NSObject : AnyObject] which I think is an NSDictionary if I am not mistaken?

Here is the code I am using but I get an error on the last line. I'm quite new to Swift so still trying to figure all this typecasting stuff out.

@IBAction func joinLobby(sender: AnyObject) {
    if let currentUser = PFUser.currentUser() {
        let channel = currentUser["language"] as! String
        self.appDelegate.client?.unsubscribeFromPresenceChannels([channel])
        self.appDelegate.client?.subscribeToChannels([channel], withPresence: true, clientState: PFUser.currentUser() as [NSObject : AnyObject])
    }
}

Error: "Cannot convert value of type "PFUser?" to type "[NSObject : AnyObject]"


Solution

  • I think the error is quite self explanatory. There is no way to implicitly convert a PFUser object to a dictionary. You need to construct your own dictionary based on the information you extract from your current user object and send that as the last argument to subscribeToChannels function.

    Also see: Converting PFObject (Parse) into JSON in Swift?