Celebrating WWDC 2022 and the 100th Daily Coding Tip 🥳
All tutorials will be free this week!

Optional binding before Swift 5.7 took the form of if let myOptional = myOptional
. Although I wanted to keep the name of the optional as I bound it as a constant inside the closure, I still have to repeat the name. In Swift 5.7 it is still possible to write something like if let myNewName = myOptional
, but it’s easier to keep the existing name without repeating yourself.
This view has an optional @State
property called displayedText
, and I am optionally binding it with if let
. When the value is equal to nil
, an empty space is provided in the form of a transparent colour. This is so that there will be a row in the Form
even if there is no value set.
The TextField
sets the inputText
property, and the Button
updates the optional displayText with this value.