views:

827

answers:

1

Here is my setup.

In my application delegate, I have a property called currentFoo. The currentFoo property is set to the currently selected Foo instance. The Foo instances each have a property which is a NSMutableArray called results. The objects in results can be of various types, NSNumber, NSString, etc...

I have an NSTableView that is displayed at certain times which allows the user to see the collection of results in the currentFoo. There is also UI to allow adding to and deleting from the results.

The NSTableView has a single column which is bound to an NSArrayController's arrangedObjects controller key. I have written a NSValueTransformer subclass that is used in this binding to convert the various possible types found in the results array to the string representation that I want shown in the table column.

The NSArrayController's content array binding is connected to an NSObjectController's selection controller key, with currentFoo.results as the model key path. Finally the NSObjectController gets its content from the application delegate.

This all works just great, adding, removing and displaying currentFoo.results in the NSTableView works just as I want.

My problem is when I try to edit the content of the table view. I have set my NSValueTransformer subclass to allow reverse transformations and have implemented reverseTransformedValue:, stepping through the code shows me that when I edit the value in the NSTableView row, it is correctly sent to that method of my value transformer, transformed and returned.

The error then occurs when the bindings try to then update the model with the reverse transformed value:

Cocoa Bindings: Error setting value for key path of object 0 (from bound object (null)): [ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key .

So basically there is this array bound to the table view, and when the user edits the row, I want to transform the string back to the appropriate object. Cocoa bindings gives me this error which leads me to believe I need to set something in the key path of the NSTableColumn binding, but I'm not sure what. I've tried "self" and that didn't work.

Any help is appreciated.

+1  A: 

The model key cannot be empty—hence the error message you get.

Change the result objects from being primitive objects (strings, boxed numbers, etc.) to model objects (of class FooResult), and set the binding's model key path to the name of the property of those model objects that you will have the table view mutate.

Basically, you can't bind directly to an array of primitive objects. You must bind to some property of an array of model objects.

Peter Hosey