title: Initialize @StateObject with a parameter in SwiftUI
body: <p>I would like to know if there is currently (at the time of asking, the first Xcode 12.0 Beta) a way to initialize a <code>@StateObject</code> with a parameter coming from an initializer.</p>
  <p>To be more specific, this snippet of code works fine:</p>
  <pre class="lang-swift prettyprint-override"><code>struct MyView: View {
    @StateObject var myObject = MyObject(id: 1)
  }
  </code></pre>
  <p>But this does not:</p>
  <pre class="lang-swift prettyprint-override"><code>struct MyView: View {
    @StateObject var myObject: MyObject
  
    init(id: Int) {
      self.myObject = MyObject(id: id)
    }
  }
  </code></pre>
  <p>From what I understand the role of <code>@StateObject</code> is to make the view the owner of the object.
  The current workaround I use is to pass the already initialized MyObject instance like this:</p>
  <pre class="lang-swift prettyprint-override"><code>struct MyView: View {
    @ObservedObject var myObject: MyObject
  
    init(myObject: MyObject) {
      self.myObject = myObject
    }
  }
  </code></pre>
  <p>But now, as far as I understand, the view that created the object owns it, while this view does not.</p>
  <p>Thanks.</p>