title: Creating a List of Toggle From a Set Using ForEach in swiftui
body: <p>I' trying to create a set of Toggles, that need to be stored in one core data field of type &quot;Transformable&quot;. I started with this example:
  <a href="https://developer.apple.com/forums/thread/118595" rel="nofollow noreferrer">https://developer.apple.com/forums/thread/118595</a>
  in combination with other ideas from stack.</p>
  <p>I'm trying to get this way:</p>
  <ul>
  <li>Create a Set of structs like this</li>
  </ul>
  <pre><code>struct AllScopes: Identifiable, Hashable {
      var id: UUID
      var name: String
      var notify: Bool
  }
  
  [...]
  // all the stuff with View and body with 
  
  @State var scopes = Set&lt;AllScopes&gt;()
  
  [...]
  // and here I run through my FetchRequest to fill the Set
  .onAppear {
      for scope in allScopes {
          scopes.insert(
              AllScopes(
                  id: scope.id!,
                  name: scope.name!,
                  notify: false
              )
          )
      }
  }
  </code></pre>
  <p>In the end I've got a nice Set with all my scopes.</p>
  <p>I call a new View with <code>YearlyReportPage6(scopes: $scopes)</code></p>
  <p>And now my problem - the next view:</p>
  <pre><code>struct YearlyReportPage6: View {
      
      @Binding var scopes: Set&lt;AllScopes&gt;
      
      init(scopes: Binding&lt;Set&lt;AllScopes&gt;&gt;) {
          _scopes = scopes
      }
      
      
      var body: some View {
          VStack {
              ForEach(scopes.indices) { index in
                  Toggle(isOn: self.$scopes[index].notify) {
                      Text(self.scopes[index].name)
                  }
              }
          }
      }
  }
  </code></pre>
  <p>But all in ForEach creates errors. Either Binding in <code>isOn:</code> is wrong, or ForEach can't work with the set, or the Text is not a String, or, or, or...
  In the end there should be a list of Toggles (checkboxes) and the selection should be stored in database.</p>
  <p>Changing the Set into a simple Array like <code>@State var scopes = [AllScopes]()</code> will work for the Toggles, but how can I store this into a <code>Transformable</code>?</p>
  <pre><code>ForEach(Array(scopes).indices) { index in
      Toggle(isOn: self.$scopes[index].notify) {
          Text(self.scopes[index].name)
      }
  }
  </code></pre>
  <p><a href="https://i.stack.imgur.com/GcJRY.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GcJRY.jpg" alt="Result how Toggles should look like" /></a></p>
  <p>To summarize:</p>
  <ul>
  <li>either how can I create the list of Toggles with the Set of AllScopes</li>
  <li>or how can I store the Array / Dictionary into the Transformable field?</li>
  </ul>
  <p>I hope, you can understand my clumsy English. :-)</p>