Saturday, June 22, 2024
HomeiOS Developmentios - SwiftUI Utilizing dynamic nested dictionary values as Binding Values -...

ios – SwiftUI Utilizing dynamic nested dictionary values as Binding Values – points with wrapping


The gist of the difficulty is that I need to filter a listing of things primarily based on numerous standards from the listing itself. For instance, I’ve a listing of meals objects, and I need to filter primarily based on the attainable values from this listing:

List view
Filters view

To assist accomplish this, I’ve nested class/structs:

class Filters {
    // Different sorts of filters, like date, and so forth.
    var listFilters: [String:ListFilter] = [:]
}

struct ListFilter {
    var title: String
    var objects: [String: FilterShowAndOn]
}

struct FilterShowAndOn {
    var showFilter: Bool
    var filterOn: Bool
}

For instance, some filters I initialize are:

func initFilters() -> Filters{
    let filters = Filters()
    
    filters.listFilters["Type"] = ListFilter(title: "Sort", objects:
                                                ["Vegetable": FilterShowAndOn(showFilter: true, filterOn: true),
                                                 "Fruit":FilterShowAndOn(showFilter: true, filterOn: true)])
    
    filters.listFilters["Color"] = ListFilter(title: "Colour", objects:
                                                ["Red": FilterShowAndOn(showFilter: true, filterOn: true),
                                                 "Green":FilterShowAndOn(showFilter: true, filterOn: true),
                                                 "Red":FilterShowAndOn(showFilter: true, filterOn: true)])
    
    return filters
}

Within the filters sheet, I need to present these and use the filterOn bool for the toggles

struct FiltersView: View {
    @Binding var filters: Filters
    
    var physique: some View {
        Part("Sort"){
            ForEach(filters.listFilters["Type"]!.objects.keys.sorted(), id:.self){ key in
                Toggle(key, isOn: $filters.listFilters["Type"].objects[key].filterOn)
            }
        }
    }
}

However I get errors irrespective of how I attempt to wrap/unwrap. Is there any option to make this work?

enter image description here

I’ve tried wrapping/unwrapping however I solely get extra errors. I am very new to swift so do not actually know what is going on on 🙁



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments