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

Question

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

Answers 1
Added at 2016-12-29 05:12
Tags
Question

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"]
Answers to

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

nr: #1 dodano: 2016-12-29 05:12

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.

Source Show
◀ Wstecz