optional closure property in Swift

Sean Danzeiser

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 by the end of the initializer. I believe I can solve this issue by declaring the var as an optional, however, I can not find the correct syntax.

How do I declare this closure property as an optional?

Jiaaro

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?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Swift optional escaping closure parameter

From Dev

Swift optional property error

From Dev

iOS Swift Pass Closure as Property?

From Dev

Swift optional chaining doesn't work in closure

From Java

How does one make an optional closure in swift?

From Dev

Swift: setting an optional property of a protocol

From Dev

Swift optional Array property is immutable?

From Dev

Swift optional Array property is immutable?

From Dev

Cocoa Binding to an optional property in swift

From Dev

What is the difference between property closure and a method in Swift?

From Dev

How to pass (optional) completion handler closure to transitionFromViewController in Swift?

From Dev

swift syntax explanation. Closure? Understand this code. Optional not unwrapping

From Dev

Create a Dictionary as a optional property using Swift

From Dev

Why is the 'view' property of UIViewController not optional in Swift?

From Dev

Swift optional or Implicit property on UIViewController subclass

From Dev

Swift - how to make inner property optional

From Dev

Explicitly unwrapping an assignment to an optional property in Swift 2.2

From Dev

Swift: Reduce optional array of models to single Bool by optional property

From Dev

optional closure and check if it is nil

From Dev

Passing optional closure to the View

From Dev

iOS/Swift: Can't assign optional String to UILabel text property

From Dev

swift - sort an array of objects by their optional boolean property without force unwrapping

From Dev

iOS/Swift: Can't assign optional String to UILabel text property

From Dev

Swift lazy stored property versus regular stored property when using closure

From Dev

Is There A Shorthand Syntax To Invoke An Optional Closure?

From Dev

How to have an optional trailing closure?

From Dev

In swift, why can I set a computed property of a polymorphic variable via optional chaining, but not on an unwrapped optional?

From Dev

In swift, why can I set a computed property of a polymorphic variable via optional chaining, but not on an unwrapped optional?

From Dev

Swift Optional of Optional

Related Related

  1. 1

    Swift optional escaping closure parameter

  2. 2

    Swift optional property error

  3. 3

    iOS Swift Pass Closure as Property?

  4. 4

    Swift optional chaining doesn't work in closure

  5. 5

    How does one make an optional closure in swift?

  6. 6

    Swift: setting an optional property of a protocol

  7. 7

    Swift optional Array property is immutable?

  8. 8

    Swift optional Array property is immutable?

  9. 9

    Cocoa Binding to an optional property in swift

  10. 10

    What is the difference between property closure and a method in Swift?

  11. 11

    How to pass (optional) completion handler closure to transitionFromViewController in Swift?

  12. 12

    swift syntax explanation. Closure? Understand this code. Optional not unwrapping

  13. 13

    Create a Dictionary as a optional property using Swift

  14. 14

    Why is the 'view' property of UIViewController not optional in Swift?

  15. 15

    Swift optional or Implicit property on UIViewController subclass

  16. 16

    Swift - how to make inner property optional

  17. 17

    Explicitly unwrapping an assignment to an optional property in Swift 2.2

  18. 18

    Swift: Reduce optional array of models to single Bool by optional property

  19. 19

    optional closure and check if it is nil

  20. 20

    Passing optional closure to the View

  21. 21

    iOS/Swift: Can't assign optional String to UILabel text property

  22. 22

    swift - sort an array of objects by their optional boolean property without force unwrapping

  23. 23

    iOS/Swift: Can't assign optional String to UILabel text property

  24. 24

    Swift lazy stored property versus regular stored property when using closure

  25. 25

    Is There A Shorthand Syntax To Invoke An Optional Closure?

  26. 26

    How to have an optional trailing closure?

  27. 27

    In swift, why can I set a computed property of a polymorphic variable via optional chaining, but not on an unwrapped optional?

  28. 28

    In swift, why can I set a computed property of a polymorphic variable via optional chaining, but not on an unwrapped optional?

  29. 29

    Swift Optional of Optional

HotTag

Archive