I’ve the next code given as the reply from this earlier query I requested:
struct Folder: Identifiable {
let id: UUID
var objects: [Int]
let kids: [Folder]
init(objects: [Int], kids: [Folder]) {
self.id = UUID()
self.objects = objects
self.kids = kids
}
var flatList: [Int] {
var objects = objects
objects.append(contentsOf: kids.flatMap { $0.flatList })
return objects
}
}
struct ContentView: View {
@State var parentFolder: Folder = Folder(objects: [0, 1, 2, 3], kids: [
Folder(items: [4, 5, 6, 7, 8], kids: [
Folder(items: [9, 10, 11, 12], kids: [])
])
])
var physique: some View {
VStack {
ForEach(parentFolder.flatList, id: .self) { merchandise in
Textual content("(merchandise)")
}
}
}
}
I wish to do extra although. As an alternative of accessing the flatList
as a replica of the information in objects
and the objects
in every little one in kids
, I might prefer to entry the objects as bindings. In different phrases, I need to have the ability to view the objects in the entire hierarchical construction as a listing, however I might like to have the ability to modify every merchandise within the listing as nicely.
I’ve tried modifying flatList
in order that it returns [Binding<Food>]
as an alternative, however I am unable to fairly get it to work. Ideally I might substitute the primary line of flatList
with one thing like
var objects = objects.map { Binding(get: { $0 }, set: { ...? }) }
However I am unsure what to exchange ...?
by.