swift-JSON字典解析
我有一个JSON文件,如下所示,我想解析它并填充我的tableview 我想得到的是“材料”、“类别”、“产品类型” 我的代码是:swift-JSON字典解析,json,swift,Json,Swift,我有一个JSON文件,如下所示,我想解析它并填充我的tableview 我想得到的是“材料”、“类别”、“产品类型” 我的代码是: var list: [String:JSON]? = [:] func loadList(){ ModafiliAPI.sharedInstance.refine(callback: { (refineList) in if let data = refineList["facets"].dictionaryValue as [String:
var list: [String:JSON]? = [:]
func loadList(){
ModafiliAPI.sharedInstance.refine(callback: { (refineList) in
if let data = refineList["facets"].dictionaryValue as [String:JSON]?{
self.list = data
self.RefineTableView!.reloadData()
print(self.refineList!)
}
})
}
我发现我可以从打印输出中访问“方面”。但在cellforrowat
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "RefineCell") as! RefineTableViewCell
cell.refineList = self.refineList?[indexPath.row] //-->>error
return cell
}
我收到以下错误:对成员“subscript”的引用不明确。
UITableViewCell:
var refineList:[String:JSON]?{
didSet{
self.setupRefineList()
}
}
Xcode不知道精炼列表是什么类型,这对他来说是不明确的 试一试 您还可以检查什么cell.refineList类型 还可以尝试:
guard let list = refineList?[indexPath.row] else {
print ("Unexpected error !!")
return cell
}
cell.refineList:[String:JSON] = list as! [String:JSON]
用手机代码更新了我的问题。添加为![String:JSON]我得到了相同的错误。您的单元格上的精炼列表是什么类型的?你能发送你的UITableViewCell代码吗?这可能会有帮助。请检查我问题的底部。你不应该在模型中使用SwiftyJSON。它只是一个出色的工具,可以将JSON解析为真实的集合类型或自定义类/结构。反序列化表视图数据源和委托方法中的对象会产生大量(不必要的)开销。并且不要将具体现有表视图的数据源数组声明为可选。
cell.refineList = self.refineList?[indexPath.row] as! [String:JSON]
guard let list = refineList?[indexPath.row] else {
print ("Unexpected error !!")
return cell
}
cell.refineList:[String:JSON] = list as! [String:JSON]