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

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,…

Contents