In this video, Scott explain how to use conditional statements to unwrap optional values into constants or variables for use only in the statement body.
- [Instructor] Optional binding allows an optional value…to be checked to see if it has a value, and if so,…that is it's not nil, it's unwrapped and assigned…to a temporary value.…I'm starting out with optional strings that do…currently have a value.…The if let statement checks an optional, and,…if it's not nil, unwraps it and assigns it…to a temporary local constant for use in its body of code.…What we're saying here is if the optional firstName…is not nil, unwrap and assign its value…to a local constant firstName.…
Notice that the local constant name…is the same name as the optional.…This is a fairly standard usage, but I could have…given the local constant name a different name…if I wanted to.…I'll continue this example by handling…if the optional is nil, in which case,…the optional bind will fail, and the else clause…will be executed.…A single if statement can contain multiple…optional bindings in a comma-separated list.…Each binding must include the let keyword,…which is a change in Swift3, because we used to be able…
Learn how to write code, understand Swift's key concepts and best practices, and strengthen your programming problem-solving skills. Instructor Scott Gardner teaches the fundamentals, so you'll be prepared to develop applications for iOS, macOS, and other platforms. Completing this course will enable you to not only write first-class code, but to think like a Swift developer.
- Creating playgrounds
- Defining variables and constants
- Working with characters and strings
- Working with collections and groups
- Using operators and defining custom operators
- Controlling program flow
- Defining functions and closures
- Working with classes, structures, and enumerations
- Adopting protocols
Skill Level Beginner
1. Get Started
2. Variables and Constants
3. Characters and Strings
4. Collections and Tuples
6. Control Flow
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
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.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.