views:

102

answers:

2

My setup is an NSTableView with one column that is binded to the NSUserDefaults. Now that works perfectly, as long as nothing gets changed in my tableview. When I trie to change a value (all simple strings) i get the following error:

Error setting value for key path of object Acid (from bound object <NSTableColumn: 0x1796ef10> identifier: path with object ID 455 in nib named TXPreferencesWindow.nib): [<NSCFString 0x165297e0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key .

Is there a simple way to make that possible? Or should I just implement the Datasource and Delegate methods? (if i did that in the first place, it would have been faster anyway...)

+1  A: 

Your error suggests that there is a problem with your binding, since the change is sent to a NSCFString object while it should be sent to a controller.

You should tell a bit more about what you're trying to bind to the table view so we can tell you how you set that up properly.

In one of my projects I've bound a NSArray of NSMutableDictionary containing a editable boolean and string to a NSTableView.

I've used a NSArrayController, which has it's content array bound to an array in the NSUserDefaultsController. The NSTableView's content is then bound to the array controller's arrangedObjects and the individual columns' value to arrangedObjects.dictionaryKey. IIRC it's import to have "Handles Content as Compound Value" selected in the controller.

Adrian
I tried to map the column value directly to the user defaults. So it looks like adding an NSArrayController would be the better way.But I changed it already to a NSTableDatasource wrapped around NSUserDefaults. Wasn't much code and then I have also perfect control.
V1ru8
+1  A: 

Bindings really doesn't like going without model objects. NSUserDefaultsController works for simple values, like binding a checkbox to a default that should have a Boolean value, but binding a table column to an array of strings will not work in any context—the table view requires model objects that it can ask for the values for the model key path. Having no model key path is what got you the error message you see (“not key-value-compliant for the key [empty string inserted].”).

Your current solution is probably the simplest. Another, which may be better for some purposes, would be to decide what these strings are, and build a model object around that. (It doesn't have to be complex. Even if all it has is a string property, there's nothing wrong with that.) Then, have an array property of these in a controller of your own creation; initialize that property from the array of strings from defaults in your initializer, and update the array of strings in defaults in the property's setter.

Peter Hosey