Hi all,
I’m new here and this is my first question, so please be kind.
I parse a json file onto a data structure that looks simplified as followed:
struct layer1: Codable {
var layer2: newLayer
}
struct newLayer: Codable {
var key1: Int
var key2: Int
...
}
This works just fine and I can access each value with e.g.
layer1.layer2.key1
Now I want to make a list view of layer2 by iterating over it:
List {
Section("Headline") {ForEach(layer1.layer2, id: \.self) {key in SelectionCell(bsp: key,
selectedItem: self.$selectedItem)}}
}
with SelectionCell as followed:
struct SelectionCell: View {
let bsp: String
@Binding var selectedItem: String?
var body: some View {
HStack {
Text(bsp)
Spacer()
Text(*value of the key*)
}
}
I guess there are several problems in there:
layer1.layer2 is no “RandomAccessCollection” How can I make it one or is there a workaround? Other method of iteration for example?
can layer1.layer2 be used as a dictionary? It looks like one. That’s why I called the variable names key and their stored data value. How can I transform it to a dictionary or how can I implement the data structure for parsing that it is a dictionary from the beginning? I guess iterating over it would then be easier.
I`m just a beginner, so every help is appreciated.
Thanks
Frank