title: SwiftUI MVVM how to loop through data and store them inside array
body: <p>I am using MVVM in my SwiftUI project and after I request to fetch data from the API, I want to loop through the data and store some of them inside an array, however it returns error, what is the correct method to do this?</p>
  <p>Here is my code and data struct</p>
  <p>MenuDetailView</p>
  <pre><code>struct MenuDetailView: View {
      @ObservedObject var viewModel = MenuDetailViewModel()
      
      var body: some View {
          ForEach(self.viewModel.variantGroup, id: \.self) { vg in
                  /*** I Need to loop and store vg.variantGroupName into array viewModel.variantChosen, how? ***/
  
                  self.viewModel.variantChosen.append(vg.VariantGroupName)
                  // This always return error:
                  // Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols
  
                  VStack {
                      HStack {
                          Text(vg.variantGroupName)
                          Text(String(self.viewModel.arrVariantChoosen[0]))
                      }
                      VStack {
                          ForEach(vg.variant, id: \.self) { v in
                              Text(v.variantName)
                          }
                      }
                  }
                  
              }
          }
      }
  }
  </code></pre>
  <p>MenuDetailViewModel.swift</p>
  <pre><code>class MenuDetailViewModel: ObservableObject, MenuDetailService {
      var apiSession: APIService
      @Published var detaildata: MenuDetailData?
      @Published var variantGroup = [MenuDetailVariantGroup]()
      @Published var variantChosen: Array&lt;String&gt; = []
      
      var cancellables = Set&lt;AnyCancellable&gt;()
      
      init(apiSession: APIService = APISession()) {
          self.apiSession = apiSession
      }
      
      func getMenuDetail() {
          let cancellable = self.getMenuDetail()
              .sink(receiveCompletion: { result in
                  switch result {
                  case .failure(let error):
                      print(&quot;Handle error: \(error)&quot;)
                  case .finished:
                      break
                  }
                  
              }) { (detail) in
                  self.detaildata = detail.data
                  self.variantGroup = detail.data.variantGroup
          }
          cancellables.insert(cancellable)
      }
      
  }
  </code></pre>
  <p>MenuDetailData.swift</p>
  <pre><code>struct MenuDetailData: Codable, Identifiable {
      let id = UUID()
      let idMenu: String
      let menuName: String
      let variantGroup: [MenuDetailVariantGroup]
  }
  </code></pre>
  <p>MenuDetailVariantGroup.swift</p>
  <pre><code>struct MenuDetailVariantGroup: Codable, Identifiable, Hashable {
      let id = UUID()
      let variantGroupName: String
      let variant: [MenuDetailVariant]
      let limit: Int
  }
  </code></pre>
  <p>MenuDetailVariant.swift</p>
  <pre><code>struct MenuDetailVariant: Codable, Identifiable, Hashable {
      let id = UUID()
      let variantName: String
  }
  </code></pre>
  <p>Thank you all in advance</p>