title: Deleting and editing specific structs in an array
body: <p>My content view is a form populated by a decoded json array like this:</p>
  <pre class="lang-swift prettyprint-override"><code>struct ContentView: View {
      
      @State var bookData: [Book] = load(&quot;list&quot;)
      @State private var selectedBook: Book? = nil
      @State private var active = false
      
      var body: some View {
          
          NavigationView {
                  Form {
                      ForEach(bookData){ bookDetail in
                          BookView(book: bookDetail)
                              .background(
                                  NavigationLink(destination: EditBook(bookData: $bookData, book: bookDetail), tag: bookDetail, selection: $selectedBook) {
                                             EmptyView()
                                          }
                                      )
                              .contextMenu{
                                  Button(action: {
                                      
                                      //delete action here
  
                                  }) {
                                      Label(&quot;Delete&quot;, systemImage: &quot;trash&quot;)
  
                                  }
                              }
                      }
                  }
                  .navigationBarTitle(&quot;Books&quot;)
                  .navigationBarItems(trailing: NavigationLink(destination: NewBook(bookData: $bookData)){Image(systemName: &quot;plus.circle&quot;).font(/*@START_MENU_TOKEN@*/.title/*@END_MENU_TOKEN@*/)})
          }
      }
  
  }
  </code></pre>
  <p>The way my app works right now is that the NewBook and Edit book views have a binder property to my bookData array. To add new book to the array the NewBook view just appends bookData with another struct and encodes it with writeJSON(bookData). Similarly in content view I want to remove a element using a context menu and reencode the edited array. For the editBook view I want to edit a struct and use writeJSON to encode it.</p>
  <p>Here is my editBook view:</p>
  <pre class="lang-swift prettyprint-override"><code>struct EditBook: View {
  
      @Binding var bookData: [Book]
      @State private var name = &quot;&quot;
      @State private var author = &quot;&quot;
      @State private var page = &quot;&quot;
      @State private var total = &quot;&quot;
      
      var book: Book
      
      var body: some View {
              Form{
                  Section(header: Text(&quot;Change the details of the book&quot;)){
                      
                      HStack{
                          Text(&quot;Name:&quot;)
                          TextField(book.name, text: $name)
                      }
                      
                      HStack{
                          Text(&quot;Author:&quot;)
                          TextField(book.author, text: $author)
                      }
                      
                      HStack{
                          Text(&quot;Page number:&quot;)
                          TextField(book.page, text: $page)
                              .keyboardType(.numberPad)
                      }
                      
                      HStack{
                          Text(&quot;Number of pages:&quot;)
                          TextField(book.total, text: $total)
                              .keyboardType(.numberPad)
                      }
                  }
                  Section{
                      HStack{
                          Spacer()
                          Button(action: {
                              
                          }) {
                              Text(&quot;Update book&quot;)
                          }
                          Spacer()
                      }
                  }
              }
              .navigationTitle(&quot;Edit Book&quot;)
      }
  }
  </code></pre>
  <p>and here is the NewBook view that works (for reference)</p>
  <pre class="lang-swift prettyprint-override"><code>struct NewBook: View {
      @Binding var bookData: [Book]
      @State private var name = &quot;&quot;
      @State private var author = &quot;&quot;
      @State private var page = &quot;&quot;
      @State private var total = &quot;&quot;
      
      var body: some View {
              Form{
                  Section(header: Text(&quot;Enter the details of the book&quot;)){
                      TextField(&quot;Title&quot;, text: $name)
                      TextField(&quot;Author&quot;, text: $author)
                      TextField(&quot;Current page number&quot;, text: $page)
                          .keyboardType(.numberPad)
                      TextField(&quot;Total number of pages&quot;, text: $total)
                          .keyboardType(.numberPad)
                  }
                  
                  Section{
                      HStack{
                          Spacer()
                          Button(action: {
                              bookData.append(Book(id: UUID(), name: name, author: author, page: page, total: total, image: &quot;string&quot;))
                              writeJSON(bookData)
                          }) {
                              Text(&quot;Add book&quot;)
                          }
                          Spacer()
                      }
                  }
              }
              .navigationTitle(&quot;New Book&quot;)
      }
  }
  </code></pre>
  <p>I tried using <code>bookData.removeAll(where: {bookData.contains(bookDetail)})</code> in the content view but that deleted everything.</p>