0

I'm beginner in swift programming and I want to assign the data to labels

so I have this code below

@IBOutlet weak var Infected: WKInterfaceLabel!
@IBOutlet weak var Cured: WKInterfaceLabel!
@IBOutlet weak var Deaths: WKInterfaceLabel!

@IBOutlet weak var OmanInfected: WKInterfaceLabel!
@IBOutlet weak var OmanCured: WKInterfaceLabel!
@IBOutlet weak var OmanDeaths: WKInterfaceLabel!
func check()
{

    // MARK: - CoronaData
    struct CoronaData: Codable {
        var countrydata: [Countrydatum]
        var stat: String
    }
    // MARK: - Countrydatum
    struct Countrydatum: Codable {
        var info: Info
        var totalCases, totalRecovered, totalUnresolved, totalDeaths: Int
        var totalNewCasesToday, totalNewDeathsToday, totalActiveCases, totalSeriousCases: Int
        var totalDangerRank: Int

        enum CodingKeys: String, CodingKey {
            case info
            case totalCases = "total_cases"
            case totalRecovered = "total_recovered"
            case totalUnresolved = "total_unresolved"
            case totalDeaths = "total_deaths"
            case totalNewCasesToday = "total_new_cases_today"
            case totalNewDeathsToday = "total_new_deaths_today"
            case totalActiveCases = "total_active_cases"
            case totalSeriousCases = "total_serious_cases"
            case totalDangerRank = "total_danger_rank"
        }
    }
    // MARK: - Info
    struct Info: Codable {
        var ourid: Int
        var title, code: String
        var source: String
    }

    if let url = URL(string: "https://api.thevirustracker.com/free-api?countryTotal=OM") {
       URLSession.shared.dataTask(with: url) { data, response, error in
          if let data = data {
              do {
                 let decoder = JSONDecoder()
                 let gitData = try decoder.decode(Countrydatum.self, from: data)
                print(gitData.totalCases as Any)
              } catch let error {
                 print(error)
              }
           }
       }.resume()
    }
}

how can I now assign these values to the labels here (Num labels)

enter image description here

I know this is a stupid question to ask but forgive me for that I just want to finish the program and learn some new things

2 Answers2

0

The countrydata is presented as an array, but the corresponding property in your model is of type String.

Curiosity
  • 544
  • 1
  • 15
  • 29
0

You can download this free program to make the struct

enter image description here

see this example to use the struct

         if let url = URL(string: "https://api.thevirustracker.com/free-api?countryTotal=OM") {
           URLSession.shared.dataTask(with: url) { data, response, error in
              if let data = data {
                  do {
                     let decoder = JSONDecoder()
                     let profile = try decoder.decode(ProfileResponse.self, from: data)
                     print(profile.countrydata.first)
                  } catch let error {
                     print(error)
                  }
               }
           }.resume()
        }

You can change the text programmatically using the setText(:) or setAttributedText(:) method.

I replicate the request in Playground the text is different bus the way that you need to access the data is the same, country data is an array so you need to get the first element

import UIKit
import PlaygroundSupport

// MARK: - ProfileResponse
struct ProfileResponse: Codable {
    var countrydata: [ProfileCountrydatum]
    var stat: String
}

// MARK: - ProfileCountrydatum
struct ProfileCountrydatum: Codable {
    var info: ProfileInfo
    var totalCases, totalRecovered, totalUnresolved, totalDeaths: Int
    var totalNewCasesToday, totalNewDeathsToday, totalActiveCases, totalSeriousCases: Int
    var totalDangerRank: Int

    enum CodingKeys: String, CodingKey {
        case info
        case totalCases = "total_cases"
        case totalRecovered = "total_recovered"
        case totalUnresolved = "total_unresolved"
        case totalDeaths = "total_deaths"
        case totalNewCasesToday = "total_new_cases_today"
        case totalNewDeathsToday = "total_new_deaths_today"
        case totalActiveCases = "total_active_cases"
        case totalSeriousCases = "total_serious_cases"
        case totalDangerRank = "total_danger_rank"
    }
}

// MARK: - ProfileInfo
struct ProfileInfo: Codable {
    var ourid: Int
    var title, code: String
    var source: String
}


class MyViewController : UIViewController {
    override func loadView() {
        let view = UIView()
        view.backgroundColor = .white

        let label = UILabel()
        label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
        label.text = "Hello World!"
        label.textColor = .black

        view.addSubview(label)
        self.view = view

        if let url = URL(string: "https://api.thevirustracker.com/free-api?countryTotal=OM") {
           URLSession.shared.dataTask(with: url) { data, response, error in
              if let data = data {
                  do {
                     let decoder = JSONDecoder()
                     let gitData = try decoder.decode(ProfileResponse.self, from: data)
                     label.text = "\(gitData.countrydata.first?.totalDeaths ?? 0)"
                  } catch let error {
                     print(error)
                  }
               }
           }.resume()
        }
    }
}
// Present the view controller in the Live View window
PlaygroundPage.current.liveView = MyViewController()

enter image description here

Enzo N. Digiano
  • 370
  • 3
  • 6
  • Thank you i did what you said what is left is to assign the data to the labels how to do that ? – The-_-King_ 464 Apr 18 '20 at 11:29
  • in place instead of doing the print you need to do Infected.setText("Your_number") https://developer.apple.com/documentation/watchkit/wkinterfacelabel/1620874-settext?language=objc – Enzo N. Digiano Apr 19 '20 at 08:16
  • Yeah I know how to do that I just want to know how to take the JSON information now and assign it to a variable how to do that – The-_-King_ 464 Apr 19 '20 at 09:14
  • let profile = try decoder.decode(ProfileResponse.self, from: data) self.Deaths.setText("\(profile.countrydata.first.totalDeaths)") – Enzo N. Digiano Apr 19 '20 at 09:33