From the course: Swift 5 Essential Training
Unlock the full course today
Join today to access over 24,000 courses taught by industry experts.
Understanding closures - Swift Tutorial
From the course: Swift 5 Essential Training
Understanding closures
- [Instructor] Closures in Swift act like blocks or Lambda expressions in other programming languages, allowing for a more concise way to write, store, and return function like operations. This is one of those things that's easier to understand in the code itself. So let's declare a variable. We're going to say this is closure, and for the type annotation, we're going to give it a closure type. The base or empty closure is two parentheses with a return type of the same. This should look familiar as it's basically a function signature. Since closures are a type in themselves, we can assign initial values. All we need is open and closed pair of curly braces, which stands in for the function's body. Now, here I've explicitly typed closure as a closure type, but the compiler can infer that from the curly braces if that's more your style. We can also see in the inspector that closure holds a function signature as its value,…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
(Locked)
Leveling the playing field30s
-
(Locked)
Basic functions3m 25s
-
(Locked)
Overloading functions3m 12s
-
Complex functions5m 9s
-
(Locked)
Function types3m 25s
-
(Locked)
Understanding closures4m 43s
-
(Locked)
Using closures7m 38s
-
(Locked)
Type aliasing4m 2s
-
(Locked)
Challenge: Battlegrounds37s
-
(Locked)
Solution: Battlegrounds5m 50s
-
(Locked)
-
-
-