Q: Is it possible to pass a function as function argument in Swift 3?
To refactor my code, I want to have a global function. At the end of this function
Yes you can pass a function/closure as parameter of another function.
This is the syntax
func doSomething(closure: () -> ()) {
closure()
}
here the function doSomething
receives as parameter a closure with no params and no return type.
You can call doSomething with this syntax
doSomething(closure: { _ in print("Hello world") })
Or using the trailing closure syntax
doSomething {
print("Hello world")
}