【发布时间】:2020-04-27 07:23:22
【问题描述】:
谁能告诉我我在这里做错了什么? 我无法将 JSON 数据从 URLSession.shared.dataTask 传递到外部扩展 也不能带来 func numberOfSections .. 等在 URLSession.shared.dataTask 中使用
感谢您的帮助,感谢您的宝贵时间。
斯威夫特
struct getSubData: 可解码 { 让 id: Int 让名称:字符串 }
结构部分:可解码{ 让 id: Int 让名称:字符串 让子数据:[getSubData] }
类 indexViewController: UIViewController { @IBOutlet 弱 var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
let url = "https://nabulsi.com/nabulsi_app/main_sections_v4.json"
let urlObj = URL(string: url)
URLSession.shared.dataTask(with: urlObj!){(data, response, error) in
do {
let sections = try JSONDecoder().decode([Section].self, from: data!)
for section in sections {
print(section.name)
let sectionName = section.name
for data in section.subData {
print(data.name)
let subSectionName = data.name
}
}
} catch {
print("We got an error")
}
}.resume()
}
}
扩展 indexViewController: UITableViewDataSource, UITableViewDelegate {
func numberOfSections(in tableView: UITableView) -> Int {
return sectionName.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sectionName[section].subSectionName?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = sectionName[indexPath.section].subSectionName?[indexPath.row]
return cell
}
}
【问题讨论】:
标签: arrays json uitableview swift5