title: CS193P SwiftUI 2020 Assignment 3 - Closure as a function parameter
body: <p>I'm following Paul Hegarty's CS193P 2020 course and the assignment 3 asks us to use a closure as a parameter for a function. That's something we already did in a previous lecture and it was quite clear for me (in fact I thought it was).
  After trying by myself I finally found the solution here: <a href="https://github.com/marlen-myn/set-game" rel="nofollow noreferrer">https://github.com/marlen-myn/set-game</a> and it works fine but I still have difficulties to understand what's happening.</p>
  <p>Here is the Model code with the SetGame init using a closure to create the CardContent.</p>
  <pre><code>struct SetGame {
      var deck: [Card]
      
      init(cardContentFactory: (Int, CardShape, CardShading, CardColor) -&gt; CardContent) {
          deck = [Card]()
          for number in 1...3 {
              for shape in CardShape.allCases {
                  for shading in CardShading.allCases {
                      for color in CardColor.allCases {
                          let content = cardContentFactory(number, shape, shading, color)
                          deck.append(Card(id: deck.count, content: content))
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  <p>In the ViewModel :</p>
  <pre><code>class SetGameViewModel: ObservableObject {
      private var setModel: SetGame
      
      init() {
          setModel = SetGame() {
              CardContent(number: $0, shape: $1, shading: $2, color: $3)
          }
          print(setModel.deck)
      }
  }
  </code></pre>
  <p>I have two questions regarding this code:</p>
  <ul>
  <li><p>I don't understand how the line <code>CardContent(number: $0, shape: $1, shading: $2, color: $3)</code> works since it seems to me it doesn't pass any &quot;actual&quot; data. In the previous lecture the example Paul used was transmitting real data. Here is the code used and it clearly passes the surfer emoji</p>
  <pre><code> func createMemoryGame() -&gt; MemoryGame&lt;String&gt; {
   return MemoryGame&lt;String&gt;(numberOfPairsOfCards: 2) { _ in &quot;‍♂️&quot; }}
  </code></pre>
  </li>
  <li><p>It seems to me that there is no real need to do this this way (with a closure). Is it because it's not done right (even if it works) or because it is really useless.</p>
  </li>
  </ul>
  <p>Thank you for your help, I really want to understand this because I know closures are everywhere in Swift and also because I hate doing something I don't understand even if it works.</p>