title: Array of binding variables for multiple toggles in MVVM pattern in SwiftUI
body: <p>I have a simple use case of having a VStack of a dynamic number of Text with Toggle buttons coming from an array.</p>
  <pre><code>import SwiftUI
  
  public struct Test: View {
          
      @ObservedObject public var viewModel = TestViewModel()
      
      public init() {
          
      }
      
      public var body: some View {
          VStack {
              ForEach(viewModel.models) { model in
                  ToggleView(title: &lt;#T##Binding&lt;String&gt;#&gt;, switchState: &lt;#T##Binding&lt;Bool&gt;#&gt;)
                  //how to add the above
              }
              
          }.padding(50)
      }
  }
  
  struct ToggleView: View {
      
      @Binding var title: String
      @Binding var switchState: Bool
      
      var body: some View {
          VStack {
              Toggle(isOn: $switchState) {
                  Text(title)
              }
          }
      }
  }
  
  public class TestModel: Identifiable {
      
      @Published var state: Bool {
          didSet {
              //do something
              //publish to the view that the state has changed
          }
      }
      @Published var title: String
      
      init(state: Bool, title: String) {
          self.state = state
          self.title = title
      }
  }
  
  public class TestViewModel: ObservableObject {
      
      @Published var models: [TestModel] = [TestModel(state: false, title: &quot;Title 1&quot;), TestModel(state: true, title: &quot;Title 2&quot;)]
     
  }
  
  </code></pre>
  <p>The following questions arise:</p>
  <ol>
  <li>In MVVM pattern, is it ok to have the binding variables in model class or should it be inside the view model?</li>
  <li>How to send the message of state change from model class to view/scene when the toggle state is changed?</li>
  <li>If using an array of binding variables in view model for each of the toggle states, how to know which particular element of array has changed? (see the following code snippet)</li>
  </ol>
  <pre><code>class ViewModel {
  
      @Published var dataModel: [TestModel]
  
  
      @Published var toggleStates = [Bool]() {
          didSet {
              //do something based on which element of the toggle states array has changed
          }
      }
  }
  </code></pre>
  <p>Please help with the above questions.</p>