So I have a model [UserModel] with nested arrays [CityModel] and within that [TownModel]. I'm pretty sure the model is setup right but I think I have the syntax wrong for trying to reference the nested arrays.
Here is my 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
}
The problem comes in my CityRow.swift, where I would just like to display the name of a city (so I can then call it in CityList and show all the cities under a user).
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])
}
}
But I get this error when trying write out my 'city' variable.
'CityModel' is not a member type of 'UserModel'
I'm sure my syntax of "var city: [UserModel.CityModel]" is incorrect, but not sure how to do it otherwise?