Skip to content

Instantly share code, notes, and snippets.

@IhwanID
Created September 24, 2020 06:25
Show Gist options
  • Save IhwanID/1ab9b3b394553466dc16f476ff95c74a to your computer and use it in GitHub Desktop.
Save IhwanID/1ab9b3b394553466dc16f476ff95c74a to your computer and use it in GitHub Desktop.
Dekontaminasi Swift ViewController code
//
// ViewController.swift
// dekontaminasi
//
// Created by Ihwan ID on 24/09/20.
//
import UIKit
class ViewController: UIViewController {
let url = "https://dekontaminasi.com/api/id/covid19/stats"
override func viewDidLoad() {
super.viewDidLoad()
getData(url: url)
}
func getData(url: String){
URLSession.shared.dataTask(with: URL(string: url)!) { (data, response, error) in
guard let data = data, error == nil else{
return
}
var result: StatsResponse?
do {
result = try JSONDecoder().decode(StatsResponse.self, from: data)
} catch {
print("Error: \(error.localizedDescription)")
return
}
guard let stats = result else{
return
}
print(stats.name)
print(stats.regions.first!)
}.resume()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment