swiftpointersparse-platformpfquerytableviewcontrolle

PFQueryTableViewController query pointer to User class and put as username label in table (Swift, Parse)


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
}

Solution

  • 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")