swiftparse-platformpfobject

Cannot Subscript A PFObject Error


I've attempted to solve this error, but I've had no luck in doing so. I'm getting the error: Cannot subscript a value of type '[PFObject]' with an index of type 'String' On this line of code: self.postDates.append(posts["createdAt"] as! String).

This is the portion of code I'm having trouble with:

var posts : [Post] = []
var postDates = [String]()

func loadData() {
    var query = PFQuery(className: "Post")
    query.orderByDescending("createdAt")

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

        if error == nil {
            if let posts = posts {
                for post in posts {
                    self.postDates.append(posts["createdAt"] as! String)
                }
                self.tableView.reloadData()
            }
        } else {
            // is an error
        }
    }
}

I'm trying to get the date and then display it every time the user create a new post utilizing Parse. Can anyone explain what is going on?

This is the tutorial I'm following along with: https://www.youtube.com/watch?v=L3VQ0TE_fjU


Solution

  • Because posts is an array of PFObject, how can you get an element inside from String? It's supposed to be an Int. It's just your typo, you already knew what you are doing. post is the PFObject you want.

    for post in posts {
        self.postDates.append(post["createdAt"] as! String)
    }