title: How to write an array of values to Firestore with SwiftUI
body: <p>Currently I have a screen that fetches a list of &quot;items&quot; from an &quot;items&quot; Collection. The user is supposed to be able to select multiple items, and add them to a document within the &quot;Movies&quot; collection when a button is pressed. I can't seem to figure out how to write the function to write the list of these items to the Movie document.</p>
  <p>Code examples below</p>
  <p>This is the loop that fetches the items (working so far)</p>
  <pre><code>    let movie: Movie
      @State var selections: [String] = []
      @ObservedObject private var viewModel = ItemsViewModel()
      @State private var isSelected = false
      @State var movieID = &quot;&quot;
      
      var body: some View {
  
                      VStack {
                          ForEach(viewModel.items.indices, id: \.self) { i in
                              Button(action: { viewModel.items[i].isSelected.toggle() }) {
                                  HStack {
                                      if viewModel.items[i].isSelected {
                                          Text(viewModel.items[i].name)
                                              .foregroundColor(.white)
                                              .font(.system(size: 16, weight: .bold))
                                          
                                          Spacer()
                                          Image(systemName: &quot;checkmark&quot;)
                                              .foregroundColor(ColourManager.pinkColour)
                                              .font(.system(size: 16))
                                      } else {
                                          Text(viewModel.items[i].name)
                                              .foregroundColor(Color(.systemGray))
                                              .font(.system(size: 16))
                                          Spacer()
                                      }
                                  }
                                  .padding(.vertical, 10)
                                  .padding(.horizontal)
                              }
                              Divider()
                          }
                      }
                      .onAppear() {
                          self.viewModel.fetchData()
                      }
  </code></pre>
  <p>Button calling function</p>
  <pre><code>Button(action: { self.addItems() }) {
  HStack {
      Text(&quot;Add Selected Items&quot;)
  }
  </code></pre>
  <p>Function to write to Firestore</p>
  <pre><code>func addItems() {
          let db = Firestore.firestore()
          db.collection(&quot;movies&quot;).document(self.movieID).setData([&quot;item&quot;: [self.selections]])
      }
  </code></pre>
  <p>What I would like is the Firestore db to look like:</p>
  <pre><code>{ 
  movieID: 12345, 
  movieName: &quot;Goodfellas&quot;, 
  items: [ 
        item: &quot;item 1&quot;,
        item: &quot;item 2&quot;
          ]
  }
  </code></pre>
  <p>So far I have the movieID and movieName working, just can't figure out how to record the selections and write them as items. Any help would be greatly appreciated!</p>