Created
September 24, 2020 06:25
-
-
Save IhwanID/1ab9b3b394553466dc16f476ff95c74a to your computer and use it in GitHub Desktop.
Dekontaminasi Swift ViewController code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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