Swift: 如何从数组中的结构访问特定数据?
Swift: How to access specific data from struct inside an array?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
text_here?.text = data[indexPath.row].properties.timeseries.description
return cell
}
struct Properties: Decodable {
let meta: Meta
var timeseries: [Timeseries]
}
struct Timeseries: Decodable {
let time: String
let data: Data
}
struct Data: Decodable {
let instant: Instant
// let next_1_hours: Next_1_hours
//let next_6_hours: Next_6_hours
//let next_12_hours: Next_12_hours
}
struct Instant: Decodable {
let details: Details
}
// MARK: - Details
struct Details: Decodable {
let air_pressure_at_sea_level: Double
let air_temperature: Double
let cloud_area_fraction: Double?
let cloud_area_fraction_high: Double?
let cloud_area_fraction_low: Double?
let cloud_area_fraction_medium: Double?
let dew_point_temperature: Double?
let fog_area_fraction: Double?
let relative_humidity: Double
let ultraviolet_index_clear_sky: Double?
let wind_from_direction: Double
let wind_speed: Double
}
在此代码行“text_here?.text = data[indexPath.row].properties.timeseries.description”。我不能进一步说明; “text_here?.text = 数据[indexPath.row].properties.timeseries.data.instant.details.air_pressure_at_sea_level.description”.
所以我的问题是如何处理数组中声明的数据,以便我可以指定它背后的数据?就像上面的代码示例一样。
先尝试下标 timeseries 数组
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
text_here?.text = data[indexPath.row].properties.timeseries[*index*]..
return cell
struct Resolution {
var width = 0
var height = 0
}
let someResolution = Resolution()
print("The width of someResolution is \(someResolution.width)")
// Prints "The width of someResolution is 0"
这就是您声明结构、初始化结构和访问其属性的方式。您可以将相同类型的结构存储在 Collection Types 中,然后使用索引访问元素。
let resolutions = [Resolution]()
resolutions.append(someResolution)
let myResolutionWidth = resolutions[0].width
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
text_here?.text = data[indexPath.row].properties.timeseries.description
return cell
}
struct Properties: Decodable {
let meta: Meta
var timeseries: [Timeseries]
}
struct Timeseries: Decodable {
let time: String
let data: Data
}
struct Data: Decodable {
let instant: Instant
// let next_1_hours: Next_1_hours
//let next_6_hours: Next_6_hours
//let next_12_hours: Next_12_hours
}
struct Instant: Decodable {
let details: Details
}
// MARK: - Details
struct Details: Decodable {
let air_pressure_at_sea_level: Double
let air_temperature: Double
let cloud_area_fraction: Double?
let cloud_area_fraction_high: Double?
let cloud_area_fraction_low: Double?
let cloud_area_fraction_medium: Double?
let dew_point_temperature: Double?
let fog_area_fraction: Double?
let relative_humidity: Double
let ultraviolet_index_clear_sky: Double?
let wind_from_direction: Double
let wind_speed: Double
}
在此代码行“text_here?.text = data[indexPath.row].properties.timeseries.description”。我不能进一步说明; “text_here?.text = 数据[indexPath.row].properties.timeseries.data.instant.details.air_pressure_at_sea_level.description”.
所以我的问题是如何处理数组中声明的数据,以便我可以指定它背后的数据?就像上面的代码示例一样。
先尝试下标 timeseries 数组
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
text_here?.text = data[indexPath.row].properties.timeseries[*index*]..
return cell
struct Resolution {
var width = 0
var height = 0
}
let someResolution = Resolution()
print("The width of someResolution is \(someResolution.width)")
// Prints "The width of someResolution is 0"
这就是您声明结构、初始化结构和访问其属性的方式。您可以将相同类型的结构存储在 Collection Types 中,然后使用索引访问元素。
let resolutions = [Resolution]()
resolutions.append(someResolution)
let myResolutionWidth = resolutions[0].width