What does the expression () -> () = {} mean in Swift?

I am going through some code Swift

and I came across this function:

func foo(withCompletion completion: @escaping () -> () = {}) { ... }

      

I'm not sure what the part means () -> () = {}

? And if this is the default, how to use it?

Any idea?

Code Swift 3

+3


source to share


1 answer


The argument completion

is of type () -> ()

. This is a closure that has no parameters and has a void return type.

= {}

is the default value for the parameter, meaning you really don't need to skip the closure if you don't need it.

Thus, you can call it like:

foo(withCompletion: {
    // your code here
})

      



or (using the closing closure syntax):

foo() {
    // your code here
}

      

or (if you don't want to use completion closure):

foo()

      

+7


source







All Articles