[Solved] Why did I get: Type ‘Any’ has no subscript members [duplicate]


You have a NSDictionary but are trying to use it as an array. What you need is an array of dictionaries.

I would recommend you changing your code a little bit.

var posts: [[String: String]] = [] // This creates an empty array of dictionaries.

override func viewDidLoad() {
    super.viewDidLoad()
    posts = [
        [ "username": "Hello" ] // This adds a dictionary as an element of an array.
    ]
}

...

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! PostCollectionViewCell
    cell.usernameLbl.text = posts[indexPath.row]["username"] // This will work now.
    cell.PictureImg.image = UIImage(named: "ava.jpg")
    return cell
}

solved Why did I get: Type ‘Any’ has no subscript members [duplicate]