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 Join a FREE crash course for iOS devs who want to become complete senior developers — from October 18th to 24th. Learn how to apply iOS app architecture patterns through a series of lectures and practical coding sessions.
Link copied to your pasteboard.