2016-12-29 47 views
1

我對swift代碼比較陌生。不能陣列的文字傳遞的plist到表視圖...無法將數組名稱傳入Swift 3的表視圖中

現在我得到一個空表視圖在模擬器

override func viewDidLoad() { 
    super.viewDidLoad() 

let path = Bundle.main.path(forResource: "xml", ofType: "plist") 
    if let dict = NSDictionary(contentsOfFile: path!), let arrays = dict.object(forKey: "Complete") as? [String: Any] { 
     self.ansTopics = arrays 
     self.chapters = self.ansTopics.keys.sorted() 
    } 

} 
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return chapters.count 
} 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "mainPage", for: indexPath) 
    cell.textLabel?.text = chapters[indexPath.row] 
    return cell 
} 

這裏是plist文件

<plist version="1.0"> 
<dict> 
<key>Complete</key> 
<dict> 
<key>Autonomic Nervous System</key> 
<array> 
    <string>Cholinergic</string> 
    <string>Anticholinergic</string> 
</array> 
<key>Peripheral Nervous System</key> 
<array> 
    <string>Central relaxants </string> 
    <string>Peripheral relaxants </string> 
</array> 
</dict> 
<key>Chap</key> 
<array> 
<string>Autonomic Nervous System</string> 
<string>Peripheral Nervous System</string> 
</array> 
</dict> 
</plist> 
+0

有什麼'self.chapters'輸出? –

+1

嘗試一次''self.chapters = self.ansTopics.keys.sorted()'line'self.tableView.reloadData()''self'tableView.reloadData()' –

+0

@NiravD空模擬器 – Nish

回答

1

試試這個代碼,它可以幫助你

let arrays = dict.object(forKey: "Complete") as? Dictionary<String,AnyObject> 

而不是

let arrays = dict.object(forKey: "Complete") as? [String: Any] 

感謝

+0

錯誤:通用參數「key」無法推斷爲轉換爲「Dictionary <__>」 – Nish

+0

as?字典

+0

Now self.ansTopics =數組的行會拋出錯誤的執行錯誤 – Nish

相關問題