title: NSMutableDictionary and new items insertion KVO
body: <p>Let me describe my problem first. I have a class which holds a NSMutableDictionary ivar. There is a thread that will add new pairs into this dictionary - at the moment of writing the application I'm not able to have a full list of available keys. An up-to-date list of all this pairs have to be shown in the Table View.
  I've prepared small proof of concept application where there is a Dictionary Controller created in the xib file, and its content is binded with the dictionary ivar. The problem is that Table View shows only initial set of the dictionary's content. After insertion it's not refreshed.</p>
  
  <p>In my proof of concept application AppController.h is as follows:</p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  
  @interface AppController : NSObject
  
  @property (strong) NSMutableDictionary *dictionary;
  
  - (NSString *)randomString;
  - (IBAction)addRandomPair:(id)sender;
  
  @end
  </code></pre>
  
  <p>And the implementation file content:</p>
  
  <pre><code>#import "AppController.h"
  
  @implementation AppController
  
  @synthesize dictionary = _dictionary;
  
  - (id)init {
      self = [super init];
      if (self) {
          _dictionary = [[NSMutableDictionary alloc] init];
          [_dictionary setValue:@"Aa" forKey:@"A"];
          [_dictionary setValue:@"Bb" forKey:@"B"];
      }
      return self;
  }
  
  - (NSString *)randomString
  {
      NSMutableString *aString = [[NSMutableString alloc] init];
      for (int i = 0; i &lt; 3; i++) {
          NSUInteger r = random() % ('z' - 'a');
          [aString appendString:[NSString stringWithFormat:@"%c", ('a' +r)]];
      }
      return aString;
  }
  
  - (IBAction)addRandomPair:(id)sender
  {
      [self.dictionary setValue:[self randomString] forKey:[self randomString]];
      NSLog([self.dictionary description]);
  }
  
  @end
  </code></pre>
  
  <p>Dictionary Controller Content is bound to the App Controller with Model Key Path set to "self.dictionary" and columns content in the Table View is bound to Dictionary Controller with Model Key Path set to key and value accordingly. In this proof of concept application button click adds a new pair (addRandomPair: action).</p>
  
  <p>I had a similar problem with a NSMutableArray and Array Controller, but there I was able to solve the problem by adding following pair of methods to the class holding an array ivar (named data in this class):</p>
  
  <pre><code>- (void)insertObject:(NSString *)object inDataAtIndex:(NSUInteger)index;
  - (void)removeObjectFromDataAtIndex:(NSUInteger)index;
  </code></pre>
  
  <p><strong>Is it possible to add some other methods to the class holding a dictionary (AppController) to be notified about new insertions? Or maybe there is a better solution for my problem?</strong></p>
  
  <h2>Update</h2>
  
  <p>I've found out that implementing following set of accessors makes Dictionary Controller notified about new item:</p>
  
  <pre><code>- (void)addDictionaryObject:(NSString *)object;
  - (void)removeDictionaryObject:(NSString *)object;
  </code></pre>
  
  <p><strong>The problem is that <code>addDictionaryObject:</code> has only one argument, dictionary would require something like <code>addDictionaryObject:forKey:</code>. Any ideas?</strong></p>
  
  <h2>Update 2</h2>
  
  <p>I don't see any other solution than using manual change notification - in this case <code>addRandomPair:</code> method looks like this:</p>
  
  <pre><code>- (IBAction)addRandomPair:(id)sender
  {
      [self willChangeValueForKey:@"dictionary"];
      [self.dictionary setValue:[self randomString] forKey:[self randomString]];
      [self didChangeValueForKey:@"dictionary"];
      NSLog([self.dictionary description]);
  }
  </code></pre>
  
  <p>It works, but I'm still a bit uncertain since dictionary itself will not change, but its content. <strong>Is using manual change notification a correct approach here?</strong></p>