It will take about 1 minutes to finish reading this article.
As the Apple official documents says, closures take one of three forms:
1. Global functions
Global functions are closures that have a name and don’t capture any values.
1 | func setupBlock { |
It is a special closure.
2. Nested functions
Nested functions are closures that have a name and can capture values from their enclosing function.
1 | func makeIncrementer() -> () -> Int { |
3. Closure expressions
Closure expressions are unnamed closures written in a lightweight syntax that can capture values from their surrounding context.
1 | { (param) -> ReturnType in |
Swift’s closure expressions have a clean, clear style, with optimizations that encourage brief, clutter-free syntax in common scenarios. These optimizations include:
- Inferring parameter and return value types from context
- Implicit returns from single-expression closures
- Shorthand argument names
- Trailing closure syntax
Reference
https://docs.swift.org/swift-book/LanguageGuide/Closures.html