ios - 无法将数组名称传递到 Swift 3 中的 TableView

标签 ios arrays swift swift3 plist

我对 swift 代码比较陌生。无法将 plist 中的数组文本传递到 TableView ...

现在我在模拟器中得到一个空 TableView

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>

最佳答案

试试这个代码,它可能对你有帮助

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

代替

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

谢谢

关于ios - 无法将数组名称传递到 Swift 3 中的 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41374307/

相关文章:

iphone - UIScrollView 动态设置页面大小

java - 使用foreach循环构造数组中的对象

ios - 将单词添加到 UITextField 时如何播放声音

arrays - 如何删除 bash 数组中的确切元素?

ios - 将swift数组转换为对象以进行json序列化

python - 从 iOS 切换 python bool 值

ios - 如何拦截 MKMapView 或 UIWebView 对象上的触摸事件?

ios - 为什么prepare for segue不能快速发送图像?

ios - 如何在 Swift 2 中为字典创建合适的单例

swift - 使用 OCR 显示事件指示器并使用线程