Learn
Great work! By using optionals, you can make sure to handle values that might be nil
. A value might be nil
because the user didn’t provide a response. A value might also be nil
because we might not have a good answer to a question (e.g. what’s the biggest number in an empty list).
In this lesson you learned how to:
- Define optionals
- Force unwrap optionals using the
!
operator - Bind optionals using
if let
statements - Bind optionals using
guard
statements - Use the nil-coalescing operator
??
to provide default values - Chain optionals using the
?
operator - Define functions that take in and return optionals
Optionals are anything but optional for writing great Swift code!
Instructions
Swift has lots of built-in methods that return optional types. Take a look at the examples here to see when you could work with more nil values.
Take this course for free
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.