Swift\'s documentation on closures states:
Swift’s closure expressions have a clean, clear style, with optimizations that encourage brief, clutter-fre
- A closure expression that is written outside of (and after) the parentheses of the function call it supports
It is just syntactic sugar to write less and is easier to read.
Giving you a use case:
You have a function that needs a another function (or closure) as a parameter like that:
func fooFunc(paramfunc: () -> Void) {
paramfunc();
}
Calling the function and giving a function as an argument is normal. Giving it a closure means the argument you give is a nameless function written between {} and with the type signature in the beginning (it is an anonymous function).
While calling a function which needs a function as argument and writing the closure after the parenthesis or omitting the parenthesis if it is the only argument (multiple trailing closures coming in Swift 5.3) makes it trailing closure syntax.
fooFunc { () -> Void in
print("Bar");
}
fooFunc() { () -> Void in
print("Bar");
}
The parenthesis after the function name can even be omitted.