ios – Easy methods to create a dictionary of which the worth is @State?


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't convert worth of sort 'Slice<Binding<[String : Bool]>>' to anticipated argument sort 'Binding<Bool>'
  • Can't convert worth of sort 'String' to anticipated argument sort '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?


Leave a Reply

Your email address will not be published. Required fields are marked *