3

How should I access columns that I've added to the User table when I have a currentUser object?

I have a PFUser.currentUser() and I want to access the nickname column that I added via the web interface.

Can I use the currentUser to get the data e.g.:

var nickname = PFUser.currentUser()["nickname"] as String

Or do I have to use a user query? e.g.:

var query = PFUser.query()
query.whereKey("username", equalTo:PFUser.currentUser().username)
var user = query.findObjects().first as PFUser
var nickname = user["nickname"]

2 Answers 2

2

If you added date to the column locally, then you have to use the first way as you wrote, or if you added date in the browser, or uploaded to parse.com some way, you have to use the second way.

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

Comments

1

I would like to give my two cents too. First of all, Daniel was right in saying that if you added the date in the browser or uploaded it to parse.com, you need to use the second way. This is an updated answer with iOS 9 and Xcode 7.2:

 var query = PFUser.query()
        query!.whereKey("username", equalTo:PFUser.currentUser()!.username!)
        do {
           user = try query!.findObjects().first as! PFUser
        } catch {
            print("Error finding user")
        }

        if user?["rankNumber"] as? Int == nil {
            user!["rankNumber"] = 0
            user!.saveInBackground()
        } else {
            print(user!["rankNumber"] as! Int)
        }

If I did it any other way, xcode would give me an error saying "failing to unwrap optional". I hope this can help someone!

Comments

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.