title: Refactoring large Swift struct with mutating functions
body: <p>I'm trying to simplify a huge file in Swift and I'm not sure what would be the best approach. It might be a very naive question as I'm still fairly new to Swift :)</p>
  <p>For the sake of simplicity, let's say I have one <code>User</code> struct and in it I can call a ton of different <code>setupModeA</code>, <code>setupModeB</code> and so on, called depending on various conditions:</p>
  <pre class="lang-swift prettyprint-override"><code>struct User {
    var currentMode: String
  
    mutating func setupModeA() {
      currentMode = &quot;A&quot;
    }
  
    mutating func setupModeB() {
      currentMode = &quot;B&quot;
    }
  
    // and so on for each mode
  }
  </code></pre>
  <p>I'd like to be able to take the <code>setupMode*()</code> functions and their associated helper functions into a separate file to make it easier to read through, since most of the time I'd be focusing on only one mode.</p>
  <p>I was thinking:</p>
  <pre class="lang-swift prettyprint-override"><code>struct ModeA {
    mutating func setup() {
      // change the mode from there
    }
  }
  </code></pre>
  <p><strong>However I can't figure out how to simply pass and mutate the <code>User</code> info to this new struct.</strong></p>
  <p>I thought about:</p>
  <ul>
  <li>initializing a bunch of <code>ModeX</code> objects with a <code>user</code> variable in the <code>User</code> struct and passing the <code>User</code> on init, but it creates weird behaviours.</li>
  <li>using static methods on the <code>ModeX</code> structs, but then I'm not quite sure how to mutate the <code>User</code></li>
  <li>taking a more functional approach &amp; not mutating the <code>User</code> but instead cloning it and re-assigning it each time... this would probably work but since my code is very sequencial it feels like added complexity / memory usage for nothing.</li>
  <li>switching from struct to classes and hoping it gives me other options... but I'm afraid everything would break</li>
  <li>really complex things that feel too weird and unintuitive to be mentioned here :)</li>
  </ul>
  <p>Any pointers would be much appreciated.</p>