Saturday, October 14, 2023
HomeiOS Developmentios - Utilizing bindings when flattening a hierarchical listing in Swift

ios – Utilizing bindings when flattening a hierarchical listing in Swift


I’ve the next code given as the reply from this earlier query I requested:

struct Folder: Identifiable {
    let id: UUID
    var gadgets: [Int]
    let kids: [Folder]
    
    init(gadgets: [Int], kids: [Folder]) {
        self.id = UUID()
        self.gadgets = gadgets
        self.kids = kids
    }
    
    var flatList: [Int] {
        var gadgets = gadgets
        gadgets.append(contentsOf: kids.flatMap { $0.flatList })
        return gadgets
    }
}

struct ContentView: View {
    
    @State var parentFolder: Folder = Folder(gadgets: [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 need to do extra although. As a substitute of accessing the flatList as a replica of the information in gadgets and the gadgets in every baby in kids, I would wish to entry the gadgets as bindings. In different phrases, I need to have the ability to view the gadgets in the entire hierarchical construction as an inventory, however I would like to have the ability to modify every merchandise within the listing as properly.

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 would change the primary line of flatList with one thing like

var gadgets = gadgets.map { Binding(get: { $0 }, set: { ...? }) }

However I am undecided what to interchange ...? by.



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments