ios – The right way to create a dictionary of which the worth is @State?

[ad_1]

I’ve tried the next (simplified extraction):

struct MyView : View {
    var names: [String]

    @State personal var flags = [String : Bool]()

    var physique: some View {
        ForEach(names, id: .self) { title in
            Toggle(isOn: $flags[name]) { <== ERRORS
                ...
            }
        }
        .onAppear {
            for title in names {
                flags[name] = false
            }
        }
    }
}

This leads to three errors:

  • Can not convert worth of kind 'Slice<Binding<[String : Bool]>>' to anticipated argument kind 'Binding<Bool>'
  • Can not convert worth of kind 'String' to anticipated argument kind 'Vary<Binding<[String : Bool]>.Index>'
  • Referencing subscript 'subscript(_:)' on 'Binding' requires that '[String : Bool]' conform to 'MutableCollection'

In all probability a foolish query, however why would not $flags[name], which is a dictionary, merely end in one worth: a Binding<Bool>?

How can this be resolved?

[ad_2]

Leave a Reply