From the course: Make SwiftUI Playgrounds Applications

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Optional values

Optional values

- [Steven] Our app is almost perfect. There's one small detail I'd like to change in our app. We used to have the surfer image when we hadn't selected a pizza. Now we have a default pizza. What I'd like is that surfer back until we select a pizza, a state that we says that we haven't selected a pizza yet, but that needs a value that means there's no value in selected pizza. Everything we've done so far has a value. How do you have that value that means no value? That is what optional variables are for. Optionals add a special value called nil. Nil stands for no value yet in this variable. I have to tell the declaration that this will be an optional value and then treat that optional differently than regular variables to detect the nil condition. If a nil is used as a value, an error occurs. Let's change selected pizza to a value of nil. So first of all, let's go hide the preview so we don't have to worry about it for a…

Contents