optional closure property in Swift

前端 未结 1 1324
再見小時候
再見小時候 2021-02-02 06:15

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

I am using this code:

    var respondToButton:(sender: UIButton) -> Bool
1条回答
  •  伪装坚强ぢ
    2021-02-02 06:42

    I believe you just need to wrap the closure type in parenthesis, like so:

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

    Alternatively if this is a closure type you're going to use often you can create a typealias to make it more readable:

    typealias buttonResponder = (sender: UIButton) -> Bool
    

    then in your class:

    var respondToButton:buttonResponder?
    

    0 讨论(0)
提交回复
热议问题