将从Json接收的数据存储在数组中,并将结果显示在Swift 4的tableView中

时间:2018-11-19 06:29:18

标签: ios swift4

响应方式如下:----

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的类型相同,但是我被卡住了!!!!!!

0 个答案:

没有答案