title: Handling Optionals/nil values in SwiftUI Views - Using if let in SwiftUI
body: <p>Views often don't allow optional parameter values, resulting in an error like <code>Initializer 'init(_:)' requires that 'String?' conform to 'StringProtocol'</code>:</p>
  
  <pre class="lang-swift prettyprint-override"><code>struct Person {
      var name : String
  }
  
  struct OptionalsExampleView: View {
  
      var person : Person? = Person(name: "Bob")
  
      var body: some View {
          VStack() {
              Text("Name:")
              Text(person?.name)
          }
  
      }
  }
  </code></pre>
  
  <p>Unfortunately, as of Xcode 11.4/iOS 13 the <code>if let</code> statement is not allowed in View Builder blocks, resulting in an error like <code>Closure containing control flow statement cannot be used with function builder 'ViewBuilder'</code>:</p>
  
  <pre class="lang-swift prettyprint-override"><code>struct OptionalsExampleView: View {
  
      var person : Person? = Person(name: "Bob")
  
      var body: some View {
          VStack() {
              if let person = person { // &lt;-- not allowed
                  Text("Name:")
                  Text(person?.name)
              }
          }
  
      }
  }
  </code></pre>
  
  <p>(I know that this question has been answered and asked quite a few times. I wrote this question and answered it myself to have a short overview article that's more concise than the existing articles so that I can link to it in places where this question comes up).</p>