UIContextualAction class has an initializer with the following signature.
public convenience init(style: UIContextualAction.Style, title: String?, handler: @escaping UIContextualAction.Handler)
An instance of the UIContextualAction class can be created using the following code snippet.
let action = UIContextualAction(style: .normal, title: "Delete") { (action, view, completion) in
completion(true)
}
What I don't understand is the way that the 3rd parameter (i.e. handler) is passed to the class initializer.
Why is the handler function is passed inside separate curly braces without passing it after passing the value for the delete parameter?
Are there any other ways which we can get the same output?
These both way of writing are the same:
let action = UIContextualAction(style: .normal, title: "Delete") { (action, view, completion) in
completion(true)
}
let action2 = UIContextualAction(style: .normal, title: "Delete", handler: { (action, view, completion) in
completion(true)
})
You can have a look at the documentation of Trailing Closures:
If you need to pass a closure expression to a function as the function’s final argument and the closure expression is long, it can be useful to write it as a trailing closure instead. A trailing closure is written after the function call’s parentheses, even though it is still an argument to the function. When you use the trailing closure syntax, you don’t write the argument label for the closure as part of the function call.