shahin ali agharia
shahin ali agharia

Reputation: 1639

Cannot subscript a value of type 'String' with an index of type 'String'

I want to display some of name on tableview cell which is stored in postFromFriends array but when i wrote this code which is shown below its give me an error of " Cannot subscript a value of type 'String' with an index of type 'String' " on declaration of name constant. If anyone can help.Thanx

var postsFromFriends = [String]()

This is my array for appending the name.

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! FriendsTaskTableViewCell

    if let name = postsFromFriends[indexPath.row]["name"] as? String {
        cell.name?.text = name
    }
    return cell
}

Upvotes: 0

Views: 1030

Answers (2)

djruss70
djruss70

Reputation: 1091

You have declared postsFromFriends as an array of String, but it sounds like you want an array of dictionary:

var postsFromFriends = [[String:String]]()

Upvotes: 1

NRitH
NRitH

Reputation: 13893

Assuming from your error message that postsFromFriends is an array of Strings, then postsFromFriends[indexPath.row] returns a String, which you're then trying to subscript with ["name"]. Did you perhaps intend your postsFromFriends array to store Dictionaries, or some custom object type that has a subscriptable name field?

Upvotes: 0

Related Questions