如何解决iOS Swift-API提取能够在控制台日志中提取数据,但无法显示
id new_id
290 190
390 190
195 195
580 150
280 150
775 775
990 620
struct WeatherResponse:可解码{ 让几何:天气 let属性:Properties 让我们输入:字符串
import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var data = [WeatherResponse]()
override func viewDidLoad() {
super.viewDidLoad()
title = "Værmelding"
downloadJson {
self.tableView.reloadData()
}
tableView.delegate = self
tableView.dataSource = self
}
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default,reuseIdentifier: nil)
cell.textLabel?.text = data[indexPath.row].geometry.type.capitalized
return cell
}
func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "showDetails",sender: self)
}
override func prepare(for segue: UIStoryboardSegue,sender: Any?) {
if let destination = segue.destination as? DetailViewController {
destination.data_detail = data[(tableView.indexPathForSelectedRow?.row)!]
}
}
func downloadJson(completed: @escaping () -> ()) {
let jsonUrlString = "https://api.met.no/weatherapi/locationforecast/2.0/compact?lat=60.10&lon=9.58"
guard let url = URL(string: jsonUrlString) else { return }
URLSession.shared.dataTask(with: url) { (data,response,err) in
print("checking for data")
if err == nil {
do {
let weather = try JSONDecoder().decode(WeatherResponse.self,from: data!)
print(weather)
DispatchQueue.main.async {
completed()
}
} catch let jsonErr {
print(jsonErr,"is error")
}
}
}.resume()
}
}
import Foundation
当我以数组形式获取API时,代码可以工作,但此处未显示。可能是因为它试图从字典中读取数据。当我从诸如以下的API中获取代码时,代码工作正常: Dota,与ID配合使用的JSON虚拟数据和API。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。