title: How to update a complicated model of a View from within its body
body: <p>I am building a custom View struct with a large and complicated model that I want to update from within the <code>var body : some View { ... }</code> property (ex. tapping on a button representing a table column in the view should change the sort order of the rows in the table).  I am not allowed to modify this model from within <code>body</code> because <code>self</code> is immutable however:</p>
  
  <pre><code>struct MyTable : View {
  
    struct Configuration {
       // a LOT of data here, ie header, rows, footer, style, etc
  
       mutating func update() { ... } // this method must be called before view render
    }
    var configuration : Configuration
  
    var body : some View {
      // build the view here based on configuration
      self.configuration.columns[3].sortAscending = false // error, `self` is immutable
      self.configuration.update() // error, `self` is immutable
    }
  }
  </code></pre>
  
  <p>I really don't want to create @State variables for all of the configuration data because 1) there's a lot of configuration data, 2) it would be difficult to model the model in such a way.</p>
  
  <p>I tried instead making <code>configuration</code> a @State var, but I am unable to set the configuration object at <code>init()</code> time even though the code compiles and runs!  (BTW, the <code>configuration</code> var now needs to be initialized before init, otherwise I get an error on the line <code>self.configuration = c</code> that <code>self</code> is used before all members are initialized -- this is likely a complication with using @State which is a property wrapper.)</p>
  
  <pre><code>struct MyTable : View {
  
    struct Configuration {
       ...
    }
  
    @State var configuration : Configuration = Configuration() // must initialize the @State var
  
    init(configuration c: Configuration) {
      self.configuration = c // does not assign `c` to `configuration` !!!
      self.$configuration.wrappedValue = c // this also does not assign !!!
      self.configuration.update()
    }
  
    var body : some View {
      // build the view here based on configuration
      self.configuration.columns[3].sortAscending = false // ok, no error now about mutating a @State var
      self.configuration.update() // ok, no error
    }
  }
  </code></pre>