从 plist 中获取数组名称作为 swift 中的表视图标签
Getting array name from plist as tableview labels in swift
这是我的 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>
只有当我将它作为字符串存储在不同的键下时,我才能获得章节名称 "chap"。
这是代码。
override func viewDidLoad() {
super.viewDidLoad()
let path = Bundle.main.path(forResource: "xml", ofType: "plist")
let dict = NSDictionary(contentsOfFile: path!)
chapters = dict!.object(forKey: "Chap") as! [String]
ansTopics = dict!.object(forKey: "Complete") as! Array<String>
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return ansTopics.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "mainPage", for: indexPath)
cell.textLabel?.text = ansTopics[indexPath.row]
return cell
}
如何获取字符串形式的数组名称并将其传递给 table视图元素?另外,如何为第二个 table 视图的相应章节索引主题(存储为字符串)?
目前,ansTopics
returns 信号 SIGABERT
错误。
问题是您的键 Complete
包含 Dictionary
而不是字符串数组。所以 ansTopics
应该声明为 [String: Any]
.
ansTopics = dict!.object(forKey: "Complete") as! [String: Any] //or Dictionary<String, Any>
如果您使用 if let
或 guard
从 Dictionary 中获取值而不是强制换行,那就更棒了。
if let dict = NSDictionary(contentsOfFile: path!), let arrays = dict.object(forKey: "Complete") as? [String: Any] {
ansTopics = arrays
}
编辑: 您需要声明一个 属性 字符串名称 chapter
类型数组的实例,并使用 keys
字典对其进行初始化然后将该数组与您的 tableview 一起使用。
var ansTopics = [String:Any]()
var chapters = [String]()
现在像这样初始化章节。
if let dict = NSDictionary(contentsOfFile: path!), let arrays = dict.object(forKey: "Complete") as? [String: Any] {
self.ansTopics = arrays
self.chapters = self.ansTopics.keys.sorted()
}
这是我的 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>
只有当我将它作为字符串存储在不同的键下时,我才能获得章节名称 "chap"。
这是代码。
override func viewDidLoad() {
super.viewDidLoad()
let path = Bundle.main.path(forResource: "xml", ofType: "plist")
let dict = NSDictionary(contentsOfFile: path!)
chapters = dict!.object(forKey: "Chap") as! [String]
ansTopics = dict!.object(forKey: "Complete") as! Array<String>
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return ansTopics.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "mainPage", for: indexPath)
cell.textLabel?.text = ansTopics[indexPath.row]
return cell
}
如何获取字符串形式的数组名称并将其传递给 table视图元素?另外,如何为第二个 table 视图的相应章节索引主题(存储为字符串)?
目前,ansTopics
returns 信号 SIGABERT
错误。
问题是您的键 Complete
包含 Dictionary
而不是字符串数组。所以 ansTopics
应该声明为 [String: Any]
.
ansTopics = dict!.object(forKey: "Complete") as! [String: Any] //or Dictionary<String, Any>
如果您使用 if let
或 guard
从 Dictionary 中获取值而不是强制换行,那就更棒了。
if let dict = NSDictionary(contentsOfFile: path!), let arrays = dict.object(forKey: "Complete") as? [String: Any] {
ansTopics = arrays
}
编辑: 您需要声明一个 属性 字符串名称 chapter
类型数组的实例,并使用 keys
字典对其进行初始化然后将该数组与您的 tableview 一起使用。
var ansTopics = [String:Any]()
var chapters = [String]()
现在像这样初始化章节。
if let dict = NSDictionary(contentsOfFile: path!), let arrays = dict.object(forKey: "Complete") as? [String: Any] {
self.ansTopics = arrays
self.chapters = self.ansTopics.keys.sorted()
}