0

I am coding an app and have user upon the user logging in I would like to take them to their dashboard and display info regarding their account. I am trying to display the username in a label but am having issues retrieving it. I am using the following code:

func getData(){
    var query: PFQuery = PFQuery(className: "User")
    query.whereKey("username", equalTo: "actualUsername")
    query.getFirstObjectInBackgroundWithBlock{
        (object: PFObject!, error: NSError!) - >Void; in
        if object != nil {
        NSLog("Success!")

        } else {

            NSLog("something went wrong")

        }

    }

I am using swift and xcode7

Ace
  • 603
  • 2
  • 15
  • 33

2 Answers2

1

You can use the currentUser property of the PFUser object. This returns the informations about the user who is currently logged in. Then you can access the username etc:

PFUser.currentUser.username
Christian
  • 22,585
  • 9
  • 80
  • 106
  • so it would be "self.usernamelabel.text = PFUser.currentUser.username" ? – Ace Oct 08 '15 at 19:12
  • I attempted that but it didnt work, im not sure if this is the proper way to do it but this worked: "var user = PFUser.currentUser() userName.text = user?.username" just incase anyone runs across this – Ace Oct 08 '15 at 21:00
0

Your problem is that the class you're querying, "User", doesn't exist. It's actually "_User", though it is more proper to use var query : PFQuery = PFUser.query() (see this answer)

Christian isn't wrong, though. If you have a user signed in, PFUser.currentUser returns the current user. So create a user PFUser variable and assign PFUser.currentUser to it, then you can access the elements using user["username"]

Community
  • 1
  • 1
Jake T.
  • 4,308
  • 2
  • 20
  • 48
  • ok so I now have var findUsers:PFQuery = PFUser.query()!; findUsers.whereKey("username", equalTo: PFUser.currentUser()!.username!) How do I place that text into the label? – Ace Oct 08 '15 at 20:45
  • Since you are displaying the information of the logged in user, querying for the user is a completely unnecessary network call. var user: PFUser = PFUser.currentUser; var username: string (or NSString? I don't know swift) = user["username"]; Your label should have a setText method. I'm not familiar with Swift, I use Obj-C, so I can't help you with the syntax of calling it. – Jake T. Oct 08 '15 at 20:47