NSObjectController and NSMutableDictionary

I’ve had nothing but trouble lately with cocoa bindings. This started out with me wanting to be able to choose from available MIDI output ports via a drop-down that used cocoa bindings. After failing miserably there, I moved on to trying to convert an Objective-C Custom View project to swift, but I couldn’t get the bindings to work. This project used an NSObjectController and it was bound to a subclass of NSObject. This object had properties red, green, blue, and alpha, but I could not hook up the properties to associated UI elements via “selection.red” (etc).

So this got me to try to do this by what apparently is the “normal” approach — to use an NSMutableDictionary as the content. There don’t seem to be many examples of doing this on the web, so I just took a stab at it.

I had to write no code at all. In the Object Controller’s Attribute Inspector, I added keys “foo” and “bar”. I bound foo to a label and a slider (bound to the object controller, Controller Key “selection”, Model Key Path “foo”). I bound bar to a label and a text field. I didn’t bother with setting up initial values for these things but it didn’t crash when I launched it. The foo elements stayed in sync (once set), as did the bar elements.

Leave a Reply

Your email address will not be published. Required fields are marked *