inessential by Brent Simmons

NSController

At the OS X conference Scott Anguish did a 90-minute presentation on the new NSController layer in Cocoa.

Here’s a quick summary of what this is about:

Many applications have three different areas of code. One handles the data—that’s called the Model layer. Another handles display—that’s the View layer. A third goes between the first two—the Controller layer.

Cocoa gives you a ton of stuff that for the first two, the Model and View layers. The hard and often tedious part has traditionally between the Controller layer.

A simple example

Imagine a checkbox in a Preferences window. When you check it or un-check it, the preferences for your application updates.

What happens is this: the user checks the box (View layer), then a message is sent to the Controller layer that a checkbox has been checked. The message tells what checkbox was checked and the method can find out whether it was turned on or off. Then that method tells the Model layer to update the on/off flag for a given preference, and it gets saved to disk in the prefs file for that app.

In Cocoa, before NSController, you had to write that method I talked about above. It’s easy but tedious. And if you think about all the different user interface pieces of even a simple app, you see that there’s a bunch of this go-between code to write.

So the idea of NSController is to do a bunch of it for you.

In this simple example, what you do is say that the checkbox is bound to a given key in the prefs. When the checkbox is checked, the new NSController layer updates the prefs. In many cases no code at all is needed.

Complex stuff is handled too

Of course, there’s more to it than just that. NSController can do a ton more than just connecting checkboxes with your preferences and eliminating a little code. It can eliminate a lot of code and handle more complex things like table views and knowing when to enable and disable things and so on.

Drawbacks

For all that coolness, there are still drawbacks—for one thing it requires Panther. That’s not a criticism, because new features come with new operating systems: that’s just the way it goes.

But if you need to support 10.2 (or earlier) in your app then you’ll have to do without NSController.

There have been criticisms of NSController: most notably, Aaron Hillegass criticized NSController in an email to the Cocoa-Dev mailing list. I’m not going to summarize Aaron’s email; instead I’ll say that any developer should think about the points Aaron makes.

Bonus tip

The documentation that comes with the developer tools CD is not as up-to-date as what’s on Apple’s website. So use the on-line docs.