0

I have an PFQueryTableView which I want to display all the open friends requests. I have a Parse Class named Follow which has the standard columns and a "from","to" and "requestStatus" column. "from" and "to" are pointers to PFUser. Now I query for all users who sent a friends request to the current User (in my queryForTable). After that I want the titleLabel of my cell to display the username of the ones who added me. I tried it like the following code shows, but it always gives me an error?! The first print() works and shows me PFUser: 0x7f9e6d809c30, objectId: 7SIxgfO00Z, localId: (null). The second print(requestingUser.username) always makes my app crash! How can I get the username to display it in my Table?

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {

    let cell = tableView.dequeueReusableCellWithIdentifier("newFriendsCell", forIndexPath: indexPath) as! newFriendsCell

    var requestingUser = object?.objectForKey("from") as! PFUser
    print(requestingUser)
    //print(requestingUser.username)


    return cell
}
rici
  • 234,347
  • 28
  • 237
  • 341
beginner_T
  • 417
  • 1
  • 6
  • 21

2 Answers2

0

You need to fetch the user before accessing the username.

requestingUser.fetchInBackgroundWithBlock({ (obj: PFObject?, error: NSError?) in
    let user = obj as! PFUser
    print(user.username)
})
Dominic K
  • 6,975
  • 11
  • 53
  • 62
0

The correct way to solve this is to add

query.includeKey("from")

to you initial query.

Your

var requestingUser = object?.objectForKey("from") as! PFUser

will then have a value.

Marius Waldal
  • 9,537
  • 4
  • 30
  • 44