Xcode error: 'Cannot assign to value: function call returns immutable value' in table views

1.1k views Asked by At

I am confused as I am defining content of rows in a table view by a dictionary, but when ever I try to add it, it shows me an error.

var places = [[String: Any]]()

class TableViewController: UITableViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    if places.count == 0 {
     places.append(["name":"Taj Mahal", "lat": "27.175277", "lon": "78.042128"])
    } 

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

    cell.textLabel?.text = places[indexPath.row]["name"]

    return cell
} 

the error is in the part where I define:

cell.textlabel?.text = places[indexPath.row]["name"]
1

There are 1 answers

1
Bista On BEST ANSWER

It should be:

places[indexPath.row]["name"] as! String

As the type is Any and you need to specify it as String, since label expects Strings.