0

In swift, how to define a function with a optional parameter who is a function? For instance, I have a function like this: func test(handler: Int -> Void) I expect the parameter handler be optional, actually, this means the handler parameter type should be optional function, so how should I do?

YonF
  • 641
  • 5
  • 20

1 Answers1

1

Just like any other type really. So for the non-optional:

func test(handler: (Int) -> (Void))

the optional version would become:

func test(handler: ((Int) -> (Void))?)

Another way to think about this is, if we were to create a typealias for the handler it would be something like:

typealias handlerCallback = (Int) -> (Void)

func test(handler: handlerCallback)  // The non-optional version
func test(handler: handlerCallback?) // The optional version

I hope that this makes sense

Alladinian
  • 34,483
  • 6
  • 89
  • 91