Sean Sean - 1 year ago 195
iOS Question

Can swift functions have a default completion handler?

In Swift, a function can have default values for a parameter like this:

func init(name: String = "foo"){...}

Can a function with a completion handler have a default value so that when calling a function there is no need to specify the completionHandler as nil, similar to the below?

func foo(completion: (success: Bool) -> void = nil){...}

Answer Source

You can either do this:

func foo(completion: (success: Bool) -> Void = {_ in }) {

Or this:

func foo(completion: ((success: Bool) -> Void)? = nil) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download