My sample function is this
func temp(type: String, callback: (() -> Void)? = nil) {
callback?()
}
But why can't I do this?
temp(type: "Here", () -> {
print("hey")
})
It says, Expected type after '->'
While this does work
temp(type: "Here", { () -> Void in
print("hey")
})
I find it weird why the () -> Void has to be inside the closure.
Even better, you can use the concise and stylish trailing closure syntax:
temp(type: "Here") {
print("hey")
}
Also, consider keeping the callback
argument label. With trailing closure syntax you don’t use it anyway, and it improves clarity if you pass a method or function name instead of a literal closure block, like this:
temp(type: "Here", callback: methodOrFunctionThatPrintsHey)