Programming in Swift: Fundamentals

Oct 19 2021 · Swift 5.5, iOS 15, Xcode 13

Part 1: Core Concepts

08. Challenge: Optionals

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 07. Optionals Next episode: 09. Conclusion

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 08. Challenge: Optionals

Update Notes: The student materials have been reviewed and are updated as of October 2021.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

Hey there, it's time for your next challenge. (banging sound) As always, you can find the challenge in the challenge optionals page of the playground you've been using, or you can download a new one from the resources for this video. Open it up and try solving the challenge questions on your own, and then keep watching to compare your work to mine. Good luck. (lively upbeat music) All right. Challenge number one. So you've been provided with a constant below, hasAllergies, which has been set to true. So below that declare an optional string variable named dog name and on the next line, use a turnery conditional operator to set the value of dog name to nil. If hasAllergies is true and to mango otherwise. All right, so, first I'm going to declare the constant instead of the true that's been done, then I declare a variable dog name and set it as an optional string. (keyboard clicking sound) So next I use a turnery conditional operator to say dog name is depending on the value of hasAllergies, nil, if true and otherwise mango. (keyboard clicking sound) There, done. On the challenge two. Challenge two. Create a constant called parsedInt and set it to int 10. So swift will attempt to parse the string 10 and convert it to an int. So place your mouse over the constant name, and then use option click to check the type of parsedInt, and why is it an optional here? Okay. First let's declare the constant and set that equal to int 10, pretends in quotes to tell swift that this is a string. (keyboard clicking sound) So why is parsedInt an optional in this case? And you and I both know that 10 as a string will easily convert to an integer. We can see that, but swift doesn't know this until it actually does the hard work of figuring out if it can convert that string value to an integer. So to be safe, swift implicitly creates parsedInt as an optional, (keyboard clicking sound) just in case it can't convert what's inside the string. Onto challenge three. Create another constant named new parsedInt instead of equal to int cat. So what will the value of newparsedInt be and why? So I'm going to create a constant newparsedInt instead of equal to int cat. (keyboard clicking sound) So here, newparsedInt is implicitly created as an optional for the same reasons as above. And I can see that here, it's an optional, but in this case, however swift can't convert cat to an integer for obvious reasons, and it sets the value for newparsedInt to nil, instead, because there's nothing else it can do. Now, that's the end of this challenge. I have a short conclusion for you to help you wrap up this part of the course and introduce you to what's next.