Cocoa Bindings – am I missing something?

in Tech Stuff

In the development of my IRC client, I have used Cocoa Bindings heavily to control my Preferences control panel. In short, I have a tree of classes – a class for an IRC Network (ModelNetwork), a class for an IRC Server (ModelServer) and a class for an IRC Channel (ModelChannel).

I have an NSMutableArray holding many ModelNetwork objects, and inside the ModelNetwork class is an NSMutableArray for a list of ModelServer obejects and an NSMutableArray for a list of ModelChannel objects.

Each of these classes know how to create an NSDictionary representation of themselves and can be initialised from an NSDictionary representation, and my application can save a Properties compatible representation of these classes to the NSUserDefaults.

Finally, each of the classes has an NSArrayController, with the bindings set up so that depending on the selection, different lists of server and channels (and the related settings) are shown. This all works great, and is very dynamic.

But the thing that strikes me is that I shouldn’t need to write any code for this at all, but trying to do this completely with bindings (no custom classes, just standard cocoa types) just won’t work.

For example, when I first prototyped this, I created the NSArrayControllers, set the class type of each to NSMutableDictionary, bound the content array of the ServersArrayController to NetworksArrayController.selection.servers, and so on – and it all worked as you expect.

But when I bound this to NSUserDefaults? It would crash and burn. It would save the structure just fine, but any value that was empty would be a nil and would be skipped when written to the defaults. On next launch, it would try to read the structure, and would fail.

What am I doing wrong here? This seems that it should work, and I should be able to write this without any code – just using bindings – but I don’t see a way.

0 Comments

Leave a Reply

Using Gravatars in the comments - get your own and be recognized!

XHTML: These are some of the tags you can use: <a href=""> <b> <blockquote> <code> <em> <i> <strike> <strong>