[英]How to get a Struct's value inside a Dictionary
新手程序员来了!
我想在字典中获取嵌套结构的值,但我无法在字典中访问。
这是我的结构
struct Forecast: Decodable {
let cod: String
let message: Int
let cnt: Int
let list: [List]
let city: City
}
struct List: Decodable {
let dt: Int
let main: Main
let weather: [Weather]
let clouds: Clouds
let wind: Wind
let sys: Sys
let dt_txt: String
private enum CodingKeys: String, CodingKey {
case dt, main, weather, clouds, wind, sys, dt_txt = "dt_txt"
}
}
struct Main: Decodable {
let temp: Double // I want to get this!
let feels_like: Double
let temp_min: Double
let temp_max: Double
let pressure: Int
let sea_level: Int
let grnd_level: Int
let humidity: Int
let temp_kf: Double
}
我试图将它作为结构语法来实现,但无法做到:
self.tempLbl.text = "\(Int(round(weather.list.main.temp)) - 273)°"
// Value of type '[List]' has no member 'main'
谢谢!
对不起我的愚蠢问题,我问这个真的很糟糕..
我可以通过以下方式到达目的地:
self.tempLbl.text = "\(Int(round(weather.list[1].main.temp)) - 273)°"
// weather.list[1].main.temp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.