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?
Asked
Active
Viewed 853 times
1 Answers
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
-
Seems to be broken in Swift 3.1 – Steven Kramer Apr 03 '17 at 15:59
-
@StevenKramer Edited. Thanks for that :) – Alladinian Apr 03 '17 at 21:36