1
votes

I have a class in Parse named Post that contains a column called "user" which points to a specific user in the User class.

How do we access the “username” string column from the User class via the “user” pointer column in the Post class...we need to retrieve the username string and set the UILabel text in the prototype cell something like so:

cell.name.text = posts["username"] as? String

where posts is a Post class PFObject. The line above doesn't work because we can't subscript the User class's "username" column directly.

1

1 Answers

0
votes

To make use of attributes of an object that you are pointing to from another object you will need to include that object in the class' query() method.

For example in your Post class you could have the following class variable and methods

var posts=[Post]()

override class func query() -> PFQuery? {  
    let query = PFQuery(className: Post.parseClassName())
    // additional query definition...
    query.orderByDescending("postDate")
    // include the user pointer column on the Post table
    query.includeKey("user")
    return query
}

Elsewhere in your app you would retrieve the Posts.

func loadPosts() {

    let query = PFQuery(className: "Post")

    query.findObjectsInBackgroundWithBlock {
        (objects: [PFObject]?, error: NSError?) -> Void in

        if error == nil {
            // The find succeeded.
            if let objects = objects {
                self.posts = (objects as? [Post])!
            }
        } else {
            // Log and notify details of the failure
        }
    }
}

You should be able to access username from each element of posts through:

let name = posts[i].user.username

Your class definitions and code would help.