title: SwiftUI - alternative to if let with a conditional closure
body: <p>I'm trying to implement the following in SwiftUI:</p>
  
  <pre><code>struct PersonView: View {
  
      @State private var age: Int? = 0
  
      var body: some View {
          VStack {
              Text("Just a test")
              if let self.age &gt; 0 {
                  Text("Display Age: \(age)")
              } else {
                  Text("Age must be greater than 0!")
              }
          }
      }
  }
  </code></pre>
  
  <p>But, in SwiftUI, <code>if let</code> results in the following error:</p>
  
  <blockquote>
    <p>Closure containing control flow statement cannot be used with function builder 'ViewBuilder'</p>
  </blockquote>
  
  <p>So after researching this topic, I came across a recommendation to use <code>.map</code> to unwrap the <code>age</code> optional.  Thus, I've modified to code within the <code>VStack</code> as follows:</p>
  
  <pre><code>Text("Just a test")
  self.age.map {elem in
      if elem &gt; 0 {
          Text("Display Age: \(elem)")
      } else {
          Text("Age must be greater than 0!")
      }
  }
  </code></pre>
  
  <p>Including a conditional within the <code>.map</code> closure, however, results in the following errors at the line calling the <code>VStack</code>:</p>
  
  <blockquote>
    <p>' (ViewBuilder.Type) -> (C0, C1) -> TupleView&lt;(C0, C1)>' requires that '()' conform to 'View'</p>
    
    <p>Type '()' does not conform to protocol 'View'</p>
  </blockquote>
  
  <p>Any suggestions for how to get past the 2nd set of errors?  Or, is there another approach for unwrapping optionals and evaluating them in SwiftUI?  Really like SwiftUI but can't believe that unwrapping optionals has been a headache!</p>