How to access user custom table columns from Parse.com

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

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

Can I use currentUser to get data like:

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

      

Or do I need to use a custom query? eg:.

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

      

+3


source to share


2 answers


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



+2


source


I would also like to give my two cents. 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 method. This is an updated answer as of 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 had done it in any other way, xcode would have given me an error message "refusing to expand optional". Hope this helps someone!

+1


source







All Articles