Bind NSTextField to dictionary

I've got a class bound to an NSArrayController to populate a table. When I tap on a row, I want a text field to be populated with the Double value for a specific dictionary key (Dictionary<String,Double> in my class).


I've added an NSDictionaryController to the storyboard, and set the "Content Dictionary" to bind to the array controller with the Controller Key set to 'selection' and the Model Key Path set to the name of my dictionary property.


I can't figure out what to set for the text field's bindings though so that I can specify a specific key from that dictionary.

Replies

Your description is not very clear. Is there a different dictionary for each row of the table? If so, the the array controller's 'selection' is either the dictionary for the selected row, or an object of which the dictionary is a property.


So, bind the text field's controller key to "selection", and its model key to whatever key path you need to get to the dictionary, plus a key for the dictionary key itself.


For example, if the selection object is the dictionary itself, the model key will be just the dictionary key.


Anyway, I'm pretty sure you don't need a NSDictionaryController at all.

Hi Quincey,


sorry for the late reply, I missed the mail that you had answered. I'm trying to simplify this a bit, so basically in my NSViewController I have a dictionary:


dynamic var maxPoints: [String : Double] = [
   "bmsw" : 0
]


It will be populated via an async network call. I've added an NSDictionaryController to the storyboard and bound the "Controller Content" to the model key path of 'maxPoints'.


Then I added an NSTextField and I bound its Value to the dictionary controller with a controller key of 'selection' and a model key path of 'bmsw'.


When I run the app, I get:


[<_NSDictionaryControllerKeyValuePair 0x61800004d380> valueForUndefinedKey:]: this class is not key value coding-compliant for the key bmsw.