Thanks to some help here I got this mostly working, but I'm not sure what the correct syntax is for how I can get SwiftUI's PreviewProvider to display the contents of my array "myDataModels".
Here is what I have:
import SwiftUI
struct Overview: View {
var myDataModels: [MyDataModel]
var body: some View {
List {
ForEach(myDataModels) { model in
Section(header: Text("\(model.firstName) \(model.lastName)")) {
ForEach(model.cities, id: \.name) { (city: City) in
Section(header: Text(city.name).fontWeight(.bold)) {
ForEach(city.towns, id: \.name) { town in
Text(town.name)
}
}
}
}
}
}
}
}
Which compiles fine, but I'm not sure what the correct means of referencing myDataModels is for the PreviewProvider call just below the above block. Here is what I have there currently.
struct Overview_Previews: PreviewProvider {
static var previews: some View {
Overview(myDataModels: userData[0])
}
}
userData is a reference to the JSON file. But I get the error:
Cannot convert value of type 'MyDataModel' to expected argument type '[MyDataModel]'
Any help us most appreciated.
userDatainstead ofuserData[0]in your preview provider?