2016-12-29 36 views
0

我很困惑,因爲我通過字典定義表視圖中的行內容,但是當我嘗試添加它時,它會顯示我錯了。Xcode錯誤:'不能賦值:函數調用返回不可變值'在表視圖

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"] 
+0

嘗試thiscell.textLabel?.text = places [indexPath.row] [「name」] as!字符串 –

回答

0

它應該是:

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

由於類型爲Any,你需要將其指定爲String,因爲label預計String s。

+0

非常感謝您回答我的問題 –