响应方式如下:----
struct Welcome: Codable { let response: Response }
struct Response: Codable { let data: [Datum] }
struct Datum: Codable { let id: Int let gmt, abbreviation, zonename:
String let region: Region let offsetSeconds: String
enum CodingKeys: String, CodingKey {
case id, gmt, abbreviation, zonename, region
case offsetSeconds = "offset_seconds"
}
}
enum Region: String, Codable { case africa = "Africa" case america = "America" case asia = "Asia" case atlantic = "Atlantic" case australia = "Australia" case e = "e" case europe = "Europe" case indian = "Indian" case pacific = "Pacific" } And I am using below code for parsing:---
session.dataTask(with: request) { (data, response, error) in
if let response = response {
print(response)
}
if let data = data {
print(data)
do {
let jsonDecoder = JSONDecoder()
let anyth = try jsonDecoder.decode(Welcome.self, from: data)
for boss in anyth.response.data {
print(boss.zonename , boss.gmt)
let biggboss = boss
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}catch {
print(error.localizedDescription)
}
}
}.resume()
我想将从API接收的数据存储在一个数组中,以便可以填充表视图。数组必须是全局的,并且与struct的类型相同,但是我被卡住了!!!!!!