【发布时间】:2020-04-16 22:15:34
【问题描述】:
我是 swift 编程的初学者,我想将数据分配给标签
所以我在下面有这段代码
@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()
}
}
我现在如何将这些值分配给此处的标签(Num labels)
我知道这是一个愚蠢的问题,但请原谅我 我只是想完成程序并学习一些新东西
【问题讨论】:
-
self.Deaths.setText("8")
标签: json swift alamofire apple-watch watchos