1

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
        }
    }
2
  • this code is working fine. Commented Jul 25, 2015 at 4:49
  • @Dharmesh Kheni Well I get an error "'AnyObject?' is not convertible to 'String'" at the line with "var email = user["email"] as! String" Commented Jul 25, 2015 at 4:53

2 Answers 2

2

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

                    }
                })
Sign up to request clarification or add additional context in comments.

2 Comments

Wow! This works perfectly. I have another question. If I have another column of profile pictures within the User class in Parse, how do I get a profile picture of that particular user?
You can get data from a parse object's key using subscripting, in the above example inside the if let, you would use var photo = user["columnNameOnParse"] as? PFFile
1
var currentUser = PFUser.currentUser()
var email = currentUser["email"]

This way you will get the email of the current user

1 Comment

If user have custom columns (Ex : age), how do you retrieve that? What can be user query?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.