Pass around closures for deferred execution, including as parameters to other function types. Learn to write closures inline using autoclosure syntax, and understand the differences between escaping and non-escaping closures.
- [Instructor] I'll keep on going in the same exercise file…from the last video.…What if you want to assign a closure…to a variable of constant,…to pass it around and use later?…Just omit the parentheses.…If the closure has multiple expression in it,…you'll need to explicitly declare the type.…But a single expression closure…can be assigned without a explicitly defining type.…I'll create a helloSayer constant,…equal to a closure that prints "Hello, world!"…And then execute that closure.…Notice that you don't write the parentheses at the end…when assigning a closure,…but instead include the parentheses…when you want to execute the closure.…
Now, I'll define a multi-statement randomNameGetter closure…and then execute it.…I have to explicitly define its type,…in this case, a function type that takes…no parameters and returns a string…and I'll call it.…I explained earlier that a function…can take another function as a parameter,…so can a function take an in line…or a stort closure as a parameter.…I've already shown you an example of in line.…
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.