Search code examples
iosswiftparse-platform

Retrieving user information from Parse in swift


I am trying to get user information (email address) from Parse. I have tried the following code, but it displays an error, saying that "Anyobject is not convertible to String". What am I supposed to do to get the email address? Thanks in advance.

  //Retrieve user info from parse 
    var query:PFQuery=PFQuery(className: "_User");

    query.whereKey("objectId", equalTo: PFUser.currentUser()!.objectId!)
    query.findObjectsInBackgroundWithBlock{
        (posts: [AnyObject]?, error: NSError?) -> Void in
        if !(error != nil) {
            let user = PFUser.currentUser()
            var email = user["email"] as! String
        }
    }

Solution

  • Since you already have a PFUser.currentUser you can just fetch that object without a query like so

    PFUser.currentUser()!.fetchInBackgroundWithBlock({ (currentUser: PFObject?, error: NSError?) -> Void in
    
    // Update your data
    
                        if let user = currentUser as? PFUser {
    
                            var email = user.email
    
                        }
                    })