title: How to bind a computed property in SwiftUI
body: <p>I have a simple model Claim:</p>
  
  <pre><code>struct Claim: Codable, Identifiable {
      let id: String
      var name: String
  
      var isSelected: Bool? = nil // used for selecting claims in a list
  }
  </code></pre>
  
  <p>The AddClaimView is for filtering out the claims that is already stored in <code>role.claims</code>, and present them in a list for the user to select for adding to the <code>role</code>.</p>
  
  <pre><code>struct AddClaimView: View {
      var claims: [Claim] // all possible claims
      @Binding var role: Role
      var claimsForAdding: [Claim] {
          self.claims.filter { claim in
              !self.role.claims.contains(claim)
          }
      }
      var body: some View {
          VStack {
              Text("Add claim")
                  .font(.largeTitle)
              ClaimsListRows(claims: self.claimsForAdding.sorted())
          }
      }
  }
  </code></pre>
  
  <p>The filtering is done by the computed property <code>claimsForAdding</code>, and here comes the problem.
  How can I bind the computed property <code>claimsForAdding</code> to <code>@Binding var claims</code> in the <code>ClaimsListRow</code> view, and then add them to <code>role.claims</code>?</p>
  
  <p><code>@State var claimsForAdding....</code> obviously doesn't work for a computed property.</p>
  
  <pre><code>struct ClaimsListRows: View {
      @Binding var claims: [Claim]
      var body: some View {
          List(claims) { claim in
  // set property isSelected = true when tapping a row
  .....
  </code></pre>
  
  <p>The view that presents <code>AddClaimView</code> in a sheet:</p>
  
  <pre><code>struct RoleDetailed: View {
      @Binding var role: Role
      @State var showAddClaim = false
      @Binding var claims: [Claim]
  
      var body: some View {
         VStack(alignment: .leading) {
              HStack {
                  Text(role.name)
                      .font(.largeTitle)
                  Spacer()
              }
              Text("id: " + role.id)
                  .font(.footnote)
                  .foregroundColor(Color.gray)
              List {
                  ForEach(role.claims) { claim in
                      ClaimRow(claim: claim)
                  }
              }
          }.font(.headline).padding(10)
          .navigationBarItems(trailing:
              Button(action: {
                  self.showAddClaim.toggle()
              }) {
                  Text("Add claim")
              }.sheet(isPresented: self.$showAddClaim, onDismiss: {
                  print("dismissed")
              }, content: {
                  AddClaimView(claims: self.claims, role: self.$role)
              })
          )
      }
  
  }
  </code></pre>
  
  <p>From the comments I have received, this is the new <code>AddClaimView</code>:</p>
  
  <pre><code>struct AddClaimView: View {
      var claims: [Claim]
      @Binding var role: Role
      var claimsForAdding: [Claim] {
          get {
              self.claims.filter { claim in
                  !self.role.claims.contains(claim)
              }
          }
          set {
              var claims = self.claims
              claims.removeAll(where: { claim in
                  newValue.contains(claim)
              })
          }
      }
      var body: some View {
          VStack {
              Text("Add claim")
                  .font(.largeTitle)
              ClaimsListRows(claims: Binding&lt;[Claim]&gt;(
                  get: {
                      self.claims.filter {
                          claim in !self.role.claims.contains(claim)
                      }
                      .sorted(by: { $0.name &lt; $1.name })
                  },
                  set: { newValue in
                      var claims = self.claims
                      claims.removeAll(where: { claim in
                          newValue.contains(claim)
                      })
                      self.role.claims = claims
                  }
              ))
          }
      }
  }
  </code></pre>