Difference between NSDictionaryController and NSArrayController

Long time, no see!
That is what I wanted to say to Cocoa Binding.
A few years ago, it was a buzz word in Mac dev. society. However, due to iPhone, issues for Mac seems not so hot as they were used to be.

So, I wanted to remind myself of basic Mac programming skills. Anyway some new classes were added to Mac OS X 10.5, and there are lots of classes I didn’t use. So, NSDictionaryController was a good candidate!

Surprisingly there was no good explanation on how to use it.
NSDictionaryController is slightly different from other “controllers”.

First, I expect that I can just get a value for a key easily from the controller.
However, the way it is used is different from what I expected.
Its arragedObjects returns keys and values as array.

Second, the way it is prepared at first was different.
Let’s take a look at some screenshots.
difference_NSDictionaryController_NSArrayController

The screenshot above is when a sample program is just launched. Any of contents for the both controllers are not prepared yet. However, for NSDictionaryController, it shows some bogus data, while NSArrayController doesn’t show anything. What makes the difference?

Let’s take a look at some more pictures.
NSDictionaryController
There are some keys defined in “Included Keys”

Hmmm.. with NSArrayController and NSObjectController, it didn’t seem to show anything when there are not data yet. Let’s confirm it.
NSArrayController
Although the keys are set in the Included Keys, they are not displayed.

To make NSDictionaryController work like NSArrayController on this, you should remove the items in “Included Keys”.

What else…?

2 responses to this post.

  1. Couple of nice things about NSDictionaryController is that it allows you to see what items are actually being used in the interface. It also has some really nice localization features. So if you base interface labels on NSDictionary keys you can easily apply localization on those keys.

    Reply

  2. Posted by jongampark on August 12, 2009 at 8:50 AM

    Hi, Jonathan~

    I saw your twitter message when I was about to leave home this morning. Thanks for your interest in this. :) ( But.. we are not allowed to use twitter at work. -.- )

    NSDictionaryController looks nice and reasonable addition to existing *Controller classes. However, I found one weird thing.

    Let’s assume that we have an NSDictionary which has (key, value) = { name, “Tom” }, { age, “35” }, { address, “Somewhere on Mars” }. In the IB, to populate a GUI widget’s content with the controller, one (key, value) should be chosen from “arrangedObjects”. For a “selected”, we put keypath to access its content.
    The NSDictionaryController treat (key, value) pairs as an array. But, in the IB’s binding property panel, I don’t see a way to select a specific pair I want. Probably there is some other “functions” like @max for this purpose. I need to look it up in Apple’s document.

    Thank you again for leaving a comment.

    How was the NSCoder night yesterday?

    Reply

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: