我在用字典定义表视图中的行内容时感到困惑,但是当我尝试添加它时,它向我显示了一个错误。
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"]
它应该是:
places[indexPath.row]["name"] as! String
由于类型为Any
,因此您需要将其指定为String
,因为label
期望String
s。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句