1

I'm making a twitter type app with Parse and Swift language.

Users are able to make posts that are displayed in a PFQueryTableViewController using the queryForTable function. One of the columns in my "Posts" class is a pointer to the "User" class and I want to be able to query this pointer and display the usernames of the users in labels next to their posts in the tableview/timeline.

How do I query this pointer and extract the username from the User class it points to as a string to put in the username label?

here is my code so far:

    override func queryForTable() -> PFQuery {

    let query = PFQuery(className: "Posts")
    query.cachePolicy = .NetworkElseCache
    query.orderByDescending("createdAt")
    return query
}

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

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

    cell.usersPostLabel.text = object?.objectForKey("postContent") as? String



    return cell
}
Matt Banks
  • 13
  • 3

1 Answers1

0

You may access the user pointer information with:

object?.objectForKey("User")!.objectForKey("username") as? String

Also, make sure you are receiving the User information on your request. So include this when querying:

query.includeKey("User")
adolfosrs
  • 9,286
  • 5
  • 39
  • 67
  • You've made my day! Thanks so much, this has been driving me mad -- I've tried so many things and all that was needed was that little line of code :) – Matt Banks Dec 06 '15 at 14:06
  • You welcome. :) Make sure to post all your future doubts, we will be realy glad on helping. – adolfosrs Dec 06 '15 at 14:09