Я новичок в Swift и знаю еще меньше об API.
Думаю, мне удалось получить некоторые данные от
[1]: https://api.met.no/weatherapi/locationf ... act_format API, но он отображается в консоли окно, в то время как я в основном пытаюсь отобразить его в своих ячейках UItableview.
Может ли кто-нибудь помочь?
Это мой файл viewController.swift:< /p>
Код: Выделить всё
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
let data = ["Nå", "Neste time", "Neste 6 timer", "Neste 12 timer"]
let subtitles = ["Temperatur:", "Vær:", "Vær:", "Vær:"]
struct WeatherManager: Codable {
let air_temperature: Int
let next_1_hours: Int
let next_6_hours: Int
let next_12_hours: String
}
override func viewDidLoad() {
super.viewDidLoad()
metAPI.shared.fetchWeatherManager()
tableView.delegate = self
tableView.dataSource = self
}
}
extension ViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("You tapped me")
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "firstCell", for: indexPath)
cell.textLabel?.text = data[indexPath.row]
cell.detailTextLabel?.text = subtitles[indexPath.row]
return cell
}
}
Код: Выделить всё
final class metAPI {
static let shared = metAPI()
func fetchWeatherManager() {
let url = URL(string: "https://api.met.no/weatherapi/locationforecast/2.0/compact.json?lat=10.74481&lon=59.91116")!
let task = URLSession.shared.dataTask(with: url) {(data, response, error) in
guard let data = data else { return }
print(String(data: data, encoding: .utf8)!)
}
task.resume()
/*let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let data = data else {
print("data was nil")
return
}
guard (try? JSONDecoder().decode(WeatherManager.self, from: data)) != nil else {
print("couldnt decode json")
return
}
}
task.resume()
}*/
}
struct WeatherManager: Codable {
let air_temperature: Int
let next_1_hours: Int
let next_6_hours: Int
let next_12_hours: String
}
}
air_temperature: Int , 10 градусов
next_1_hours: Int, Дождь 4 мм
next_6_hours: Int, Облачно 3 мм
next_12_hours: String, SUN
Кто-нибудь может мне немного помочь?
Подробнее здесь: https://stackoverflow.com/questions/645 ... right-side
Мобильная версия