Updated for Xcode 14.2
In these chapters we’ve covered one of Swift’s most important features, and although most people find optionals hard to understand at first almost everyone agrees they are useful in practice.
Let’s recap what we learned:
if letto run some code if the optional has a value, or
guard letto run some code if the optional doesn’t have a value – but with
guardwe must always exit the function afterwards.
??, unwraps and returns an optional’s value, or uses a default value instead.
try?– you’ll either get back the function’s return value, or
nilif an error is thrown.
Optionals are second only to closures when it comes to language features folks struggle to learn, but I promise after a few months you’ll wonder how you could live without them!
SPONSORED Thorough mobile testing hasn’t been efficient testing. With Waldo Sessions, it can be! Test early, test often, test directly in your browser and share the replay with your team.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.