Cocoa Binding to an optional property in swift

Igor Vidrevich

How do I setup cocoa bindings for properties that are of optional datatype?

I have an NSTableView that displays data from NSArrayController via key bindings, which in turn is managing objects for an array of [Properties] which are defined as:

    class Property: NSObject {
        var distance: CLLocationDistance    
        var ETA: TimeInterval?                
        var coordinates: CLLocationCoordinate2D
        // other properties and methods here
    }

in MainViewController I define

    var properties: [Property] = []
    @IBOutlet var arrayController: NSArrayController!

where arrayController is bound to properties array, NSTableView is bound to Array Controller (Controller Key = arrangedObjects) and then individual TableViewCells bound to various properties of Table Cell View (model key path = objectValue.distance, etc)

The code compiles without any issues, but at runtime it crashes when it tries to load the table. The issue is with the ETA column since properties.ETA is an optional. What is the proper way of dealing with this issue? Thank you

rickster

You can't, at least not directly.

Cocoa Bindings work through the Objective-C runtime, and ObjC has no way to represent Swift Optionals that aren't of object (aka class aka reference) type. (That is, you can access an NSWindow? from ObjC, or even an AnyObject?, but not an Int? or a CGRect? or a TimeInterval?.)

Your best bet here is probably to fall back to doing things the way you would in ObjC — make ETA a non-optional TimeInterval, and use some sentinel value (negative something-huge?) to represent cases where an ETA is not set. You can use value transformers to make sure such cases get represented appropriately in your UI.

Alternately, you could keep the optional property for use in Swift, and expose a second, computed property of non-optional type for use in bindings.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

optional closure property in Swift

From Dev

Create a Dictionary as a optional property using Swift

From Dev

How is optional binding used in swift?

From Dev

Swift optional Array property is immutable?

From Dev

Cocoa Binding for bit mask property uses check boxes

From Dev

Swift optional property error

From Dev

Swift Optional Binding with a negative result

From Dev

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

From Dev

Swift: setting an optional property of a protocol

From Dev

Optional binding syntax in Swift

From Dev

Equivalence of short-cut optional binding syntax in Swift

From Dev

Bound value in a conditional binding must be of Optional type in Swift

From Dev

Swift Bound value in a conditional binding must be of Optional type

From Dev

How to use optional binding in Swift 2

From Dev

swift 2 initializer for conditional binding must have Optional type, not 'UIImage'

From Dev

Binding must have optional type Swift

From Dev

Swift 2 Unable to remove optional binding

From Dev

Swift optional or Implicit property on UIViewController subclass

From Dev

Swift: Cocoa binding value to a computed property does not work

From Dev

Swift 3: 'if let' optional binding error

From Dev

Optional binding bug on Swift 2.2?

From Dev

Swift optional Array property is immutable?

From Dev

Cocoa Binding to NSTableView selection

From Dev

Swift optional binding only allows constants

From Dev

Swift - how to make inner property optional

From Dev

Swift 2 Unable to remove optional binding

From Dev

Swift optional binding still having null data

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

Related Related

  1. 1

    optional closure property in Swift

  2. 2

    Create a Dictionary as a optional property using Swift

  3. 3

    How is optional binding used in swift?

  4. 4

    Swift optional Array property is immutable?

  5. 5

    Cocoa Binding for bit mask property uses check boxes

  6. 6

    Swift optional property error

  7. 7

    Swift Optional Binding with a negative result

  8. 8

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

  9. 9

    Swift: setting an optional property of a protocol

  10. 10

    Optional binding syntax in Swift

  11. 11

    Equivalence of short-cut optional binding syntax in Swift

  12. 12

    Bound value in a conditional binding must be of Optional type in Swift

  13. 13

    Swift Bound value in a conditional binding must be of Optional type

  14. 14

    How to use optional binding in Swift 2

  15. 15

    swift 2 initializer for conditional binding must have Optional type, not 'UIImage'

  16. 16

    Binding must have optional type Swift

  17. 17

    Swift 2 Unable to remove optional binding

  18. 18

    Swift optional or Implicit property on UIViewController subclass

  19. 19

    Swift: Cocoa binding value to a computed property does not work

  20. 20

    Swift 3: 'if let' optional binding error

  21. 21

    Optional binding bug on Swift 2.2?

  22. 22

    Swift optional Array property is immutable?

  23. 23

    Cocoa Binding to NSTableView selection

  24. 24

    Swift optional binding only allows constants

  25. 25

    Swift - how to make inner property optional

  26. 26

    Swift 2 Unable to remove optional binding

  27. 27

    Swift optional binding still having null data

  28. 28

    Explicitly unwrapping an assignment to an optional property in Swift 2.2

  29. 29

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

HotTag

Archive