Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Getting User's Username from Parse.com

I am trying to get the username of a PFUser. The code below works without this line:var username = nameId.username as String but when it is added the collection view does not show any results. Any ideas?

    var query = PFQuery(className:"Chat")
        //        query.whereKey("user", equalTo:currentUser)
        query.whereKey("rideId", equalTo:currentObjectId)
        query.orderByAscending("createdAt")

        query.findObjectsInBackgroundWithBlock {
            (objects: [AnyObject]!, error: NSError!) -> Void in
            if error == nil {
                // The find succeeded.
                NSLog("Successfully retrieved \(objects.count) scores.")
                // Do something with the found objects
                for object in objects {
                    NSLog("%@", object.objectId)

                    var testId = object.objectId

                    println(testId)

                    self.orderedIdArray.append(testId)

                    var message = object.objectForKey("message") as String
                    self.messageString = message
                    self.messageArray.append(self.messageString)
                    println(message)

                    var nameId = object.objectForKey("user") as PFUser
                    var username = nameId.username as String
                    self.nameString = username
                    self.namesArray.append(self.nameString)

                    self.collectionView?.reloadData()

                }

            } else {
                // Log details of the failure
                NSLog("Error: %@ %@", error, error.userInfo!)
            }

        }

2 Answers

It is now working with this line:

query.includeKey("user")
var nameId = object.objectForKey("user") as PFUser
var username = nameId.username as String

on your backend is the "user" field a pointer to the user object? or is it just the userID? if its the userID you need to run another query to get that userID.

var userQuery = PFUser.query()
userQuery.getObjectInBackgroundWithId(nameId) { 
  (object: PFObject!, error: NSError!) -> Void in
  if error == nil {
        let user: PFUser = object as? PFUser
       //user is a PFUSer
       var username = user.username as String
       self.nameString = username
       self.namesArray.append(self.nameString)


  } else {
    NSLog("%@", error)
  }
}

It is a Pointer I believe. Should the code I am using work then?

yes if it is a pointer your code should work. double check, print out nameId and see what it says. if it just prints out the id then its not a pointer to the user object

When those lines of code are not included then the message is printed. When that code is added, it stops after the first message, before it gets to print the username. Also on the server it says Pointer<_User>

when you say it stops does it crash? what do you mean it stops

It does not crash, It just does not run any code after the line

var username = nameId.username as String

so if you write

 var username = user.username as String
println("username: \(username")

it doesnt print anything out at all? thats very strange.

Yes. This is the code:

                    var nameId = object.objectForKey("user") as PFUser

                    println("Name Id is:")
                    println(nameId)

                    var username = nameId.username as String
                    println("username: \(username)")

And it prints:

2014-12-02 17:47:52.882 GoRide[6976:1168365] 5xYKslv5Me
Object id is: 5xYKslv5Me
Message is: My Message
<PFUser:8Ef4GSbLqi:(null)> {
}

then nothing else. There are 8 objects that it should display.