Join Harrison Ferrone for an in-depth discussion in this video Unwrapping optionals, part of Learning Swift 3 for Developers.
- [Instructor] So now that we have optional variables,…how do we get at their values?…I'm going to make some new space underneath…our previous section and add a new header comment.…Unwrapping optionals.…For this example, I'm going to declare a new variable…called wrapped optional.…This is going to be an optional integer,…and have an initial value of three.…If we print this out to the console now,…we'll get the value three,…but cast as an optional.…If you tried to put this on scree in an app,…the entire optional parenthesis three would appear,…and that would be really awkward.…
You can see on the left side of the editor,…that x code is trying to help us fix our unwrapping problem.…Select the yellow triangle and look at the options…the editor is giving us.…The better option in this case,…is to set a default value.…Having said that, force unwrapping optionals…should be done with great care.…It will be safer for your sanity with a large code base…if you avoided force unwrapping entirely.…From my point of view,…force unwrapping sort of defeats the purpose…
- Starting a new playground
- Printing to the console
- Declaring variables
- Working with numbers
- Using strings
- Breaking down optionals
- Understanding closures
- Classes vs. structs
- Extensions and protocols
Skill Level Beginner
1. Hello Swift
2. Back to Basics: Variables and Operators
3. Collections, Tuples, and Enumerations
4. Working with Logic Flows
5. Only in Swift: Optionals
6. Functions, Closures, and Associated Types
7. Classes vs. Structs
8. Advanced Topics
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.