title: SwiftUI - How to set a non static var from a static function
body: <p>I'm currently following a <a href="https://cs193p.sites.stanford.edu/" rel="nofollow noreferrer">series of lectures from Stanford</a> to learn Swift (Code is from lectures 1-4 plus the homework I'm trying to complete) and am having an issue with setting an instance var(non-static) from a static function. I have gotten this to work but I would like it to be able to have different instances of the game have different themes</p>
  <pre><code>class EmojiMemoryGame: ObservableObject {
      
      //Create the model in the viewController
      @Published private var cardGame = EmojiMemoryGame.createMemoryGame()
      
      static let themesArray = [
          theme(name: &quot;Halloween&quot;, color: Color.orange, emojiArray: [&quot;&quot;,&quot;&quot;,&quot;&quot;,&quot;&quot;,&quot;&quot;,&quot;&quot;,&quot;&quot;])
              {6},
          theme(name: &quot;Aqua&quot;, color: Color.blue, emojiArray: [&quot;‍☠️&quot;,&quot;&quot;,&quot;&quot;,&quot;⚓️&quot;,&quot;&quot;,&quot;&quot;,&quot;⛵️&quot;,&quot;&quot;,&quot;&quot;,&quot;&quot;]) {Int.random(in: 5...7)},
          theme(name: &quot;Animals&quot;, color: Color.gray, emojiArray: [&quot;&quot;,&quot;&quot;,&quot;&quot;,&quot;&quot;,&quot;&quot;,&quot;&quot;,&quot;&quot;,&quot;&quot;])
              {Int.random(in: 3...4)}
      ]
      
      static var selectedTheme: theme?
      
      //Public Static Function
      //Input: None
      //Output: MemoryGame&lt;String&gt; struct
      //Usage: Return the card game with emoijs inited
      static func createMemoryGame() -&gt; MemoryGame&lt;String&gt; {
          
          //ISSUE
          //Want to get a random theme from the themes array and assign it to an instance of this class but get an error beceause the function is static
          selectedTheme = themesArray[Int.random(in: 0..&lt;themesArray.count)]
          let emojisArray = selectedTheme!.emojiArray
          
          //Create the actual MemoryGame
          return MemoryGame&lt;String&gt;(numberOfPairsOfCards: selectedTheme!.numberOfCards(), cardContentFactory: { cardPairIndex in
              emojisArray[cardPairIndex]
          })
      }
      
      //Struct Theme
      struct theme {
          let name: String
          let color: Color
          let emojiArray: [String]
          let numberOfCards: () -&gt; Int
      }
  </code></pre>
  <p>I want to be able to get a random <code>theme</code> from <code>themesArray</code> but let <code>selectedTheme</code> be an instance var. Can anyone give me some advice?</p>