我正在尝试从JSON内容(在我的data.swift文件中)获取一些数据并将其分配给"comments".任何人都知道这里出了什么问题以及如何解决它?看起来像我遇到麻烦的语法问题.
我得到的错误:

import UIKit
class CommentsTableViewController: UITableViewController {
var story = [String:AnyObject]()
var comments = [String:AnyObject]()
override func viewDidLoad() {
super.viewDidLoad()
comments = story["comments"]
tableView.estimatedRowHeight = 140
tableView.rowHeight = UITableViewAutomaticDimension
}
Run Code Online (Sandbox Code Playgroud)
它不喜欢这个comments = story["comments"]部分.
Dar*_*ren 26
您的代码中存在错误,但由于Swift编译器错误,您看到的错误消息不正确且具有误导性.实际的错误消息应为:AnyObject is not convertible to [String:AnyObject].
self.story["comments"]返回一个AnyObject.要为self.comments您分配该值,必须首先将类型转换AnyObject为Dictionary类型[String:AnyObject].
例如:
self.comments = self.story["comments"] as! [String:AnyObject]
Run Code Online (Sandbox Code Playgroud)