2017-02-25 156 views
0

我有一個像下面這樣的JSON文件,我想解析它並填充我的tableview。swift - JSON字典解析

我想要得到的是 「材料」, 「類別」, 「product_types」

["facets": { 
     "material" : { 
     "data" : [ 
      { 
      "count" : 3, 
      "value" : "95% Polyester, 5% Spandex" 
      }, 
      { 
      "count" : 1, 
      "value" : "%100 Modal" 
      } 
     ], 
     }, 
     "categories" : { 
     "data" : [ 
      { 
      "id" : "7", 
      "name" : "test" 
      } 
     ], 
     }, 
     "product_types" : { 
     "data" : [ 
      { 
      "count" : 3, 
      "value" : "Sweatshirt" 
      }, 
      { 
      "count" : 1, 
      "value" : "Babet" 
      }, 
     ], 
     } 
    }] 

我的代碼是:

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 
    } 

我收到以下錯誤:Ambiguous reference to member 'subscript'

的UITableViewCell:

var refineList:[String:JSON]?{ 
     didSet{ 
      self.setupRefineList() 
     } 
    } 
+1

模型,則不應使用SwiftyJSON。這只是將JSON解析爲* real *集合類型或自定義類/結構的絕妙工具。它會產生很多不必要的開銷來反序列化表視圖數據源和委託方法中的對象。並且不要將具體現有表視圖的數據源數組聲明爲可選項。 – vadian

回答

0

的Xcode不知道是什麼類型refineList,它的曖昧他

cell.refineList = self.refineList?[indexPath.row] as! [String:JSON] 

你也可以檢查cell.refineList類型是什麼?

相關搜索:

guard let list = refineList?[indexPath.row] else { 
     print ("Unexpected error !!") 
     return cell 
} 
cell.refineList:[String:JSON] = list as! [String:JSON] 
+0

用單元代碼更新了我的問題。添加爲! [字符串:JSON]和我得到相同的錯誤。 –

+0

您的手機上的refineList的類型是什麼?你可以發送你的UITableViewCell代碼,它可以是有幫助的。 – iLandes

+0

請檢查我的問題的底部。 –