如何在struct模型中访问枚举? 迅速

我想在我的tableView中显示现在结构模型中的数据和时间。

我尝试访问查看主要的WeatherModel结构但是有一个[List]放弃了我访问dtTxt。 我只是想在每一行显示数据。 任何帮助,将不胜感激。 我在下面添加我的内容。

控制器:

@IBOutlet var tableView: UITableView! var weatherData = [WeatherModel]() override func viewDidLoad() { super.viewDidLoad() getJSONData { self.tableView.reloadData() } tableView.delegate = self tableView.dataSource = self } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return weatherData.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = UITableViewCell(style: .default, reuseIdentifier: nil) cell.textLabel?.text = weatherData[indexPath.row].city.name.capitalized // Instead of city name, it should be dtTxt. return cell } 

这是我的模型:

 struct WeatherModel: Codable { let list: [List] let city: City } struct City: Codable { let name: String } struct List: Codable { let main: Main let weather: [Weather] let dtTxt: String // I want to show this enum CodingKeys: String, CodingKey { case main, weather case dtTxt = "dt_txt" //access here } } struct Main: Codable { let temp: Double } struct Weather: Codable { let main, description: String } 

这是我的JSON对象:

  { "list": [ { "main": { "temp": 277.12 }, "weather": [{ "main": "Clouds", "description": "scattered clouds" }], "dt_txt": "2018-06-05 15:00:00" }, { "main": { "temp": 277.12 }, "weather": [{ "main": "Sunny", "description": "Clear" }], "dt_txt": "2018-06-05 18:00:00" }, { "main": { "temp": 277.12 }, "weather": [{ "main": "Rain", "description": "light rain" }], "dt_txt": "2018-06-05 21:00:00" } ], "city": { "name": "Bishkek" } } 

[List]包含不同时间的预测对象数组。

使用for循环:

 for forecast in weatherModel.list { print(forecast.dtTxt) } 

或按特定条件过滤一个对象:

 if let threePMForecast = weatherModel.list.first(where: { $0.contains("15:00") }) { print(threePMForecast.dtTxt) }