35

Cocoa for Mac OS X support Cocoa Binding technology, reducing need of glue code between your views and your models.

Recreating Cocoa Bindings on top of KVC on KVO seems doable with the help from cocotron source code.

Is there any technical or conceptual reason why Cocoa Bindings are not part of the SDK ? I'm thinking about performance, lack of garbage collection, lack of KVC support from UI elements (UISlider for instance).

Olivier Tabone
  • 1,081
  • 8
  • 6
  • 3
    Cocoa bindings existed long before garbage collection, so you can rule that out. – Brad Larson Jan 16 '11 at 19:58
  • 5
    I have created a simple (i.e no value transformers) bindings-like mechanism: see NSObject+SimpleBindings in https://github.com/mruegenberg/objc-utils/tree/master/CoreFoundationAdditions No interface builder support at the moment, but it does help in simplifying code. – mrueg Mar 22 '11 at 20:23

1 Answers1

9

Bindings on the desktop requires:

  1. All suitable UI components to expose bindings for their important properties
  2. Implementation of NSArrayController infrastructure for handling selection

On iOS, point 2 becomes less of an issue. It is rare (and perhaps even a bad UI design) to have a master-detail view layout where multiple selection is possible. This is where NSArrayController really comes into its own.

Bindings for a UITableView become less practical as the current NSArrayController design doesn't stretch to multiple sections, titles etc.

Point 1 would be useful to have. However it requires a large quantity of work by Apple to implement bindings support in all the UIKit controls. I assume it's never become a high enough priority for them to implement.

Mike Abdullah
  • 14,933
  • 2
  • 50
  • 75
  • 2
    Late to this party, but don't forget NSTreeController. Though you might wish you could (ba-dum tish). Actually they did fix it. But it could have handled tableview sections I think. Also, multiple selection is currently simulated in edit mode. Given the limits of touch interfaces (no modifier keys), that's what we're stuck with. But I think an array controller would be great on the iPad with master-detail when you want to edit a filtered list. – Bored Astronaut Aug 20 '11 at 01:35
  • 5
    I frequently bind my `@properties` to checkboxes and textfields in my UI for Mac OS X apps... it'd be nice to be able to do that in iOS. I feel like maybe the reason Apple hasn't included it is because they're rather difficult to debug... the stack trace you get when you mess up a Cocoa Binding is full of calls within the SDK making it rather difficult to find out what you've done wrong. – ArtOfWarfare Dec 09 '12 at 15:36
  • @ArtOfWarfare note that actually you bind your controls to other things, not the other way round – Mike Abdullah Dec 12 '12 at 21:58
  • Re: NSArrayController. Why wouldn't NSFetchedResultsController be good enough to replace NSArrayController? – Z S May 03 '13 at 02:24
  • @ZS Potentially, yes. It's still an awkward fit though, `NSFetchedResultsController` doesn't have the concept of a selection. Mind you, that should probably not be needed on iOS anyway, or at least a lot of the time – Mike Abdullah May 03 '13 at 14:56
  • It's quite a lot of work yes, but it's doable, see [AKA Beacon](https://github.com/mutech/aka-ios-beacon), this does not yet cover core data, xml and collection sources well (coming) but most of the core UI – Michael Nov 30 '15 at 02:34
  • 1
    fast forward to 2017 and iPad Pro, still no bindings in iOS 11... – adib Sep 10 '17 at 10:26
  • But we do have NSFetchedResultsController on macOS 10.12. Demo of using it with NSTableView: https://developer.apple.com/library/content/samplecode/Earthquakes/Listings/Objective_C_Earthquakes_AAPLQuakesViewController_m.html – malhal Dec 05 '17 at 14:52
  • In the end - no reasonable answer for the "Why". Cocoa-Bindings, when tastefully used, dramatically reduce the amount of code (synchronising views with model values) and make synchronisation reliable. They also prevent many UI bugs that otherwise crawl in as the programmer adds features, neglecting to synch UI in obscure scenarios. iPhone 8 (let alone X) is better a computer than my 2004 Mac which had Cocoa-bindings. Question of porting existing Apple Controllers from MacOS to iOS is... just funny. Of course they can. So who is that person who blocks this technology from getting into iOS? – Motti Shneor Mar 11 '19 at 07:01
  • My guess it wasn't implemented because the main iOS UI component UITableView re-uses its cells, you would need to manually bind and unbind the UITableViewCell's subviews. And since some of the cell data is static instead of binding you might as well just set the data and then you don't need to worry about unbinding on cell reuse. So there would be no point in adding data binding to the other widgets that appear on cells because it would never be used. 3rd party impls don't have suppressSpecificNotificationFromObject which is how a user change is stopped from going to model and back again. – malhal Oct 30 '20 at 09:23