Swift's optional close property

How do you declare an optional closure as a property in Swift?

I am using this code:

var respondToButton:(sender: UIButton) -> Bool 

but the compiler complains that the property is not initialized at the end of the initializer. I believe I can solve this problem by declaring var as optional, however I cannot find the correct syntax.

How to declare this closing property as optional?

+47
ios swift
Jun 11 '14 at 18:38
source share
1 answer

I believe that you just need to wrap the closure type in parentheses, for example:

 var respondToButton:((sender: UIButton) -> Bool)? 



Alternatively, if this is the closure type that you often use, you can create typealias to make it more readable:

 typealias buttonResponder = (sender: UIButton) -> Bool 

then in your class:

 var respondToButton:buttonResponder? 
+74
Jun 11 '14 at 18:47
source share



All Articles