title: Conditions inside init() in SwiftUI
body: <p>I need to use init with SwiftUI, but I am just learning about initializers. I have found many complex examples, but I cannot understand the basics.</p>
  <p>How to initialize different values inside init() based on value of some other var, for example based on true/false from a toggle?</p>
  <p>EXAMPLE CODE</p>
  <p>I was trying something like this:</p>
  <pre><code>import SwiftUI
  
  struct ToggleView: View {
      
      @State var toggleValue: Bool = true
      
      var testValue: String
      
      init() {
          if toggleValue {
              self.testValue = &quot;true&quot;
          } else {
              self.testValue = &quot;false&quot;
          }
      }
      
      var body: some View {
          
          VStack {
              Toggle(isOn: $toggleValue) {
                  Text(&quot;True or false?&quot;)
              }.padding()
              
              Text(&quot;Selected value:&quot;)
              Text(&quot;\(testValue)&quot;)
          }
      }
  }
  
  struct ToggleView_Previews: PreviewProvider {
      static var previews: some View {
          ToggleView()
      }
  }
  </code></pre>
  <p>But on line with <strong>if toggleValue {</strong> I am getting an error:</p>
  <blockquote>
  <p>'self' used before all stored properties are initialized</p>
  </blockquote>
  <p>I have tried changing:</p>
  <pre><code>var testValue: String
  
  init() {
  </code></pre>
  <p>to:</p>
  <pre><code>@Binding var testValue: String
  
  init(testValue: Binding&lt;String&gt;) {
  </code></pre>
  <p>But it doesn't work either. How to bind this testValue with toggle, so after taping on toggle my app would print: &quot;Selected value: false&quot;?</p>