macoscontrollernsdictionarycocoa-bindingsnstableview

Add objects to NSDictionaryController


I have an NSTableView which is bound to an NSDictionaryController. I have set this and the content dictionary through the interface builder. The question I have is, how do I add objects to the dictionary in a manner that the controller will see it automatically and display it in the table.

I've seen in examples about NSArrayController that you add the new objects through the controller rather than the actual array so the controller sees the change. However, I don't see a similar way of doing this with the dictionarycontroller...

Thanks for the help.


Solution

  • Well I ended up figuring this out. I figured since the arraycontroller mirrored the use of an array that a dictionarycontroller would do the same but it is not so. The process is a little bit different...

    So if you have a dictionarycontroller that you would like to add values to so it can update anything binding to it you use the following process:

    //Creates and returns a new key-value pair to represent an entry in the 
    //content dictionary.
    id newObject = [dictController newObject];
    //Use NSDictionaryControllerKeyValuePair Protocol setKey 
    [newObject setKey:@"Key"];
    //Use NSDictionaryControllerKeyValuePair Protocol setValue
    [newObject setValue:@"value"];
    //Add the object to the controller
    [dictController addObject:newObject];
    

    This will in effect, update both the dictionarycontroller and add the value to the content dictionary. You cannot use setValue:ForKey in this case because this is not a method implemented in the NSDictionaryControllerKeyValuePair Protocol.

    I hope this can help others using these controllers!