-1

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

Lucas Huang
  • 3,998
  • 3
  • 20
  • 29
mur7ay
  • 803
  • 3
  • 16
  • 44

2 Answers2

0

You are trying to get (and add) the created at date of the PFObject,
instead you are getting the date of and array of PFObject (Which Posts is).

You should try to get the elements in the array, and get the date from the element instead of the array.

for post in posts{
    postDates.append(post["createdAt"] as! String)
}
milo526
  • 5,012
  • 5
  • 41
  • 60
0

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)
}
Lucas Huang
  • 3,998
  • 3
  • 20
  • 29