Quantcast
Channel: Active questions tagged rest - Stack Overflow
Viewing all articles
Browse latest Browse all 3641

API fetch (Able to fetch data in console log, but can't display it)

$
0
0
import UIKitclass 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 struct WeatherResponse: Decodable {let geometry: Weatherlet properties: Propertieslet type: String} // MARK: - Geometry struct Weather: Decodable {    let type: String    let coordinates: Array<Double>} //MARK: - Propertiesstruct Properties: Decodable {    let meta: Meta    //let timeseries: [Timesery]} //MARK: - Meta struct Meta: Decodable {    let updated_at: String    let units: Units}// MARK: - Unitsstruct Units: Decodable {    let air_pressure_at_sea_level: String    let air_temperature: String    let cloud_area_fraction: String    let precipitation_amount: String    let relative_humidity: String    let wind_from_direction: String    let wind_speed: String}

The code works when I fetch APIs as an array, but here it doesn't display. It might be because it is trying to read data from as a Dictionary. The code works fine when I fetch from APIs such as; Dota, JSON dummy data and APIs that works with id.


Viewing all articles
Browse latest Browse all 3641

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>