【问题标题】:Xcode error: 'Cannot assign to value: function call returns immutable value' in table viewsXcode 错误:表视图中的“无法赋值:函数调用返回不可变值”
【发布时间】:2016-12-29 04:38:19
【问题描述】:

我很困惑,因为我正在通过字典定义表格视图中的行内容,但是当我尝试添加它时,它会显示一个错误。

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
} 

错误在我定义的部分:

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

【问题讨论】:

  • 试试这个cell.textLabel?.text = places[indexPath.row]["name"] as!字符串

标签: swift2 ios9 xcode7.3


【解决方案1】:

应该是:

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

由于类型为Any,您需要将其指定为String,因为label 需要Strings。

【讨论】:

  • 非常感谢您回答我的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多