title: Change values in struct model
body: <p>I'm stuck with adding items from struct to favorites. The idea:</p>
  <ul>
  <li>I have a json with data for cards</li>
  <li>On the main screen app shows a random card</li>
  <li>The user could pick another random card or save it to favorites.</li>
  </ul>
  <p>Code below.</p>
  <ol>
  <li>Creating a CardModel (file 1):</li>
  </ol>
  <pre><code>struct CardModel: Hashable, Codable, Identifiable {
      let id: Int
      let topic: String
      let category: String
      var saved: Bool
  }
  </code></pre>
  <ol start="2">
  <li>Retrieving data from json and creating an array of structs  (file 2):</li>
  </ol>
  <pre><code>var cardsModelArray: [CardModel] = load(&quot;LetsTalkTopics.json&quot;)
  
  </code></pre>
  <ol start="3">
  <li>Func to pickup random item from the array (file 3):</li>
  </ol>
  <pre><code>func pickRandomCard() -&gt; CardModel {
      let randomCard = cardsModelArray.randomElement()!
      return randomCard
  }
  </code></pre>
  <ol start="4">
  <li>Func to change the &quot;saved&quot; bool value (file 4)</li>
  </ol>
  <pre><code>func saveCard(card: CardModel) {
      let index = card.id
      cardsModelArray[index] = CardModel(id: index, topic: card.topic, category: card.category, saved: !card.saved)
  }
  </code></pre>
  <ol start="5">
  <li>View file (file 5, simplified)</li>
  </ol>
  <pre><code>import SwiftUI
  
  struct StackOverFlow: View {
      
      @State var currentCard = pickRandomCard()
      
      var body: some View {
          VStack{
              CardViewStackOver(cards: currentCard)
              Button(&quot;Show random card&quot;) {
                      currentCard = pickRandomCard()
              }
              
              Button(&quot;Save item&quot;) {
                  saveCard(card: currentCard)
              }
      }
  }
  
  struct StackOverFlow_Previews: PreviewProvider {
      static var previews: some View {
          NavigationView {
              StackOverFlow()
          }
      }
  }
  
  
  struct CardViewStackOver: View {
      
      let cards: CardModel
      
      var body: some View {
          VStack {
              Text(cards.topic)
              Text(cards.category)
              Text(String(cards.id))
              HStack {
                  if cards.saved {
                      Image(systemName: &quot;heart.fill&quot;)
                          .font(.title)
                          .padding(15)
                          .padding(.bottom, 20)
                  } else {
                      Image(systemName: &quot;heart&quot;)
                          .font(.title)
                          .padding(15)
                          .padding(.bottom, 20)
                  }
              }
          }
      }
  }
  
  </code></pre>
  <p>But I definitely making something wrong, in a separate view I'm showing saved cards but it doesn't work (it shows some random cards, and some saved cards have duplicates). With my research, I found out that structs are immutable and when I'm trying to edit a value, basically swift creates a copy of it and makes changes in the copy. If so, what would be the right approach to create this favorite feature?</p>