title: How do I set an @State variable programmatically in SwiftUI
body: <p>I have a control that sets an @State variable to keep track of the selected tabs in a custom tab View. I can set the @State variable in code by setting the following:</p>
  
  <pre><code>@State var selectedTab: Int = 1
  </code></pre>
  
  <p>How do I set the initial value programmatically so that I can change the selected tab when the view is created?</p>
  
  <p>I have tried the following:</p>
  
  <p>1:</p>
  
  <pre><code>@State var selectedTab: Int = (parameter == true ? 1 : 2)
  </code></pre>
  
  <p>2:</p>
  
  <pre><code>init(default: Int) {
      self.$selectedTab = default
  }
  </code></pre>