How to define in swift an optional variable for a function type, which can be nil?

swift

Solution

You just have to wrap it in parentheses:

var OnClick: ((UIButton!) -> ())? = nil

Problem

I would like to have a variable to hold a function, but which is initialized to nil. I'm trying to make it an optional, but I get an error. ``` var OnClick: (UIButton!) -> ()? = nil ``` I get this error ``` Could not find an overload for '__conversion' that accepts the supplied arguments ``` If I remove the ?, I also get the same error.

Original source