Cocoa Bindings Tips – Array Controllers

Working with Array Controllers I’ve learned a few things that I’d like to write down.

1)  You can subclass and override an NSArrayController’s awakeFromNib file to set your own sortDescriptors.  When you do this, you can around the issue of a NSTableView not being sorted until you click on a column header.

2)  Content, Content Objects, Content Values explained:

Binding a NSPopUpCell to a collection of objects can be tricky.  I had the case where I have enumerated values, so ultimately we are talking about setting a numeric value for a property in the data model, but want to display user friendly text.  One would think “bind the number then use a one-way value transformer to convert those enums to strings.”  Somehow this didn’t work like I’d hoped.  In the end I used an array of NSDictionary objects and did the bindings as follows:

Content binds an array controller’s arranged objects.  But each of these will be an NSDictionary.  (Don’t forget in the NSArrayController’s inspector in Interface Builder, so specify that objects for that controller are of type NSDictionary, and specify the keys you use in those dictionaries in that same pane)

Content Objects – this is like saying, from the Content, is there a specific property that you are trying to bind to your data model?  What you set here is the object that will be selected.

Content Values – what would you like to display?

Selected Object – what are you binding the selected object to?

So, in this case I bound content to an array of NSDictionaries, the Content Object was that dictionary with key “numberRepresentation”, and the contentValue was “textRepresentation”, so that I would be displaying text, but assigning a NSNumber – the selected contentObject – to the parameter on the bound data model (as specified by the selected Object binding).

This seems pretty trivial, but was very helpful getting my head around Core Bindings.  It’s a powerful technology, but it’s unforgiving as it isn’t easy to debug, and it doesn’t provide much in terms of debug info / what went wrong.  The error messages are often not that helpful.


One thought on “Cocoa Bindings Tips – Array Controllers

Leave a Reply

Fill in your details below or click an icon to log in: Logo

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

Google photo

You are commenting using your Google 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 )

Connecting to %s