Riks
Riks

Reputation: 37

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

Upvotes: 1

Views: 1857

Answers (2)

ajay goel
ajay goel

Reputation: 104

var currentUser = PFUser.currentUser()
var email = currentUser["email"]

This way you will get the email of the current user

Upvotes: 1

Fred Faust
Fred Faust

Reputation: 6790

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

                    }
                })

Upvotes: 2

Related Questions