title: How to use Dictionary as @Binding var in SwiftUI
body: <p>I will need to display a collapsed menu in SwiftUI, it is possible to pass one single bool value as binding var to subviews but got stuck when trying to pass that value from a dictionary.</p>
  
  <p>see code below:</p>
  
  <pre><code>struct MenuView: View {
      @EnvironmentObject var data: APIData
      @State var menuCollapsed:[String: Bool] = [:]
      @State var isMenuCollapsed = false;
  
  // I am able to pass self.$isMenuCollapsed but self.$menuCollapsed[menuItem.name], why?
  
  var body: some View {
  
              if data.isMenuSynced {
                  List() {
                      ForEach((data.menuList?.content)!, id: \.name) { menuItem in
                          TopMenuRow(dataSource: menuItem, isCollapsed: self.$isMenuCollapsed)
                              .onTapGesture {
                                  if menuItem.isExtendable() {
                                      let isCollapsed = self.menuCollapsed[menuItem.name]
                                      self.menuCollapsed.updateValue(!(isCollapsed ?? false), forKey: menuItem.name)
                                  } else {
                                      print("Go to link:\(menuItem.url)")
                                  }
  
                              }
                      }
                  }
              }else {
                  Text("Loading...")
              }
          }
  
  }
  </code></pre>
  
  <p>in ChildMenu Row:</p>
  
  <pre><code>struct TopMenuRow: View {
      var dataSource: MenuItemData
      @Binding var isCollapsed: Bool
  
      var body: some View {
          ChildView(menuItemData)
              if self.isCollapsed {
                  //display List of child data etc
              }
          }
      }
  }
  </code></pre>
  
  <p>If I use only one single bool as the binding var, the code is running ok, however, if I would like to use a dictionary to store each status of the array, it has the error of something else, see image blow:
  <a href="https://i.stack.imgur.com/Rvqj1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Rvqj1.png" alt="enter image description here"></a></p>
  
  <p>if I use the line above, it's fine.</p>
  
  <p>Any idea of how can I fix it?</p>
  
  <p>Thanks</p>