【发布时间】:2019-10-29 23:58:44
【问题描述】:
所以我有一个模型 [UserModel],其中包含嵌套数组 [CityModel] 和该 [TownModel]。我很确定模型设置正确,但我认为尝试引用嵌套数组的语法错误。
这是我的 UserModel.swift:
import SwiftUI
struct UserModel: Codable, Identifiable {
let id: Int
let firstName: String
let lastName: String
let cities: [CityModel]
enum CodingKeys: String, CodingKey {
case id
case firstName = "first_name"
case lastName = "last_name"
case cities
}
}
struct CityModel: Codable {
let name: String
let towns: [TownModel]
}
struct TownModel: Codable {
let name: String
}
问题出现在我的 CityRow.swift 中,我只想在其中显示一个城市的名称(这样我就可以在 CityList 中调用它并显示一个用户下的所有城市)。
struct CityRow: View {
var city: [UserModel.CityModel]
var body: some View {
VStack(alignment: .leading) {
Text(city.name)
.font(.headline)
}
}
}
struct CityRow_Previews: PreviewProvider {
static var previews: some View {
CityRow(city: userData[0])
}
}
但在尝试写出我的“城市”变量时出现此错误。
'CityModel' is not a member type of 'UserModel'
我确定我的“var city: [UserModel.CityModel]”语法不正确,但不知道该怎么做?
【问题讨论】:
标签: arrays json swift model swiftui