1

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?

1 Answer 1

1

Your CityRow needs a city in order to call its name. CityModel is not a Member of UserModel but cities is. I don't know your app or what you're trying to achieve but e.g. in a list you want to present every city thats in the array. Your CityRow needs var city: CityModel as a constructor. It could look like this (again, I have no idea what you're trying to achieve, some more info/code would be helpful!).

Here's your CityRow

struct CityRow: View {
    var city: CityModel

    var body: some View {
        VStack(alignment: .leading) {
            Text(city.name)
                .font(.headline)
        }
    }
}

And here's the ForEach Loop that you could use in a list or something

ForEach(user.cities) {city in
    CityRow(city: city)
}

If you could edit your question with some more code, like the view that presents all the CityRows, it would be helpful!

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for this, I started editing the post to kinda fill in the full extent of the help I needed and realized it was probably more appropriate as a separate post — which I have made here stackoverflow.com/questions/58635853/…
@FuegoDeBassi i answered there, even though there's another answer. The other answer could work but i did some adjustments. Have a nice day!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.