Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- class HomeViewController: UIViewController {
- var covidStatData: CovidStat?
- override func viewDidLoad() {
- super.viewDidLoad()
- print("viewDidLoad(): covidStatData before getCovidStat(): \(covidStatData)")
- getCovidStat()
- print("viewDidLoad(): covidStatData after getCovidStat(): \(covidStatData)")
- }
- func getCovidStatJSON(completion: @escaping (String?, Error?) -> Void) {
- print("getCovidStatJSON(completion:) start")
- let zipCd = User.currentUser?.zipCd
- let apiURL: String = "https://localcoviddata.com/covid19/v1/locations?zipCode=\(zipCd ?? "")"
- guard let cURL = URL(string: apiURL) else { return }
- let request = URLRequest(url: cURL)
- URLSession.shared.dataTask(with: request) { (data, response, error) in
- print("dataTask(with:) closure")
- if let str = String(data: data!, encoding: .utf8) {
- completion(str, nil)
- }
- }.resume()
- print("getCovidStatJSON(completion:) end")
- }
- func getCovidStat() {
- print("getCovidStat start")
- getCovidStatJSON(completion: { covidStatJSON, error in
- print("getCovidStatJSON(completion:) closure")
- if let covidStatJSON = covidStatJSON {
- let covidStatData = Data(covidStatJSON.utf8)
- let decoder = JSONDecoder()
- do {
- print("Will set covidStatData")
- self.covidStatData = try decoder.decode(CovidStat.self, from: covidStatData)
- print("Did set covidStatData")
- //Now that we set the value, let's use it
- DispatchQueue.main.async {
- //self.myLabel.text = self.covidStatData.zipCd
- //...
- }
- } catch {
- print(error)
- }
- }
- })
- print("getCovidStat end")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement