title: Editable table view bound to NSArrayController bound to NSUserDefaultsController
body: <p>I have a simple model <code>Foo</code> that represents a user preference and encapsulates an <code>NSString</code> and an <code>NSNumber</code>. I want to store an array of <code>Foo</code>’s in user defaults so that they persist between launches of the application; and I’d like to display them in an table view such that the user can add, remove, and edit them. The solution to this seemed pretty straightforward with bindings, but it’s proving impossible for me to actually get working.</p>
  
  <p>When my application launches I register with <code>NSUserDefaults</code> a keyed archive of an array of initial <code>Foo</code>’s. In my XIB I have an array controller with its content array bound to the shared user defaults controller’s <code>values</code> controller key; with a key path of <code>foos</code>; ‘Handles Content As Compound Value’ checked; and <code>NSKeyedUnarchiveFromData</code> as the value transformer. The table view is then in turn bound to the array controller and the columns of the table to the properties of <code>Foo</code>.</p>
  
  <p>This works perfectly when <code>Foo</code>’s are added and removed from the array—the <code>foos</code> key in user defaults is updated to reflect the new contents of the array. The problem is that changes to the properties of an individual <code>Foo</code> don’t similarly trigger the array to be written back to user defaults. I believe the reason for this is discussed in the ‘<a href="https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/KeyValueObserving/Articles/KVODependentKeys.html#//apple_ref/doc/uid/20002179-SW5" rel="nofollow noreferrer">To-many Relationships</a>’ section of ‘Registering Dependent Keys’ in the Key-Value Observing Programming Guide: changes to the array controller’s array itself are observed, but changes to properties of the contained elements aren’t.</p>
  
  <p>Despite recognizing this as the problem I can’t for the life of me figure out what to actually do in order to make this work. When I observe a change on a <code>Foo</code> what object do I need to inform and with what message such that the entire array is written back to user defaults—<code>NSUserDefaults</code>, <code>NSUserDefaultsController</code>, <code>NSArrayController</code>? I wish the Apple documentation had more than a cursory mention of this problem and provided some actual example code to solve it—I’ve tried everything and I can’t find the magic incantation.</p>
  
  <p>This is similar to <a href="https://stackoverflow.com/questions/973208/how-to-get-notified-of-changes-to-models-via-an-nsarraycontroller">How to get notified of changes to models via an NSArrayController</a> but I can’t make the conceptual leap between what he’s doing with a custom view and the use of <code>bind:toObject:withKeyPath:options:</code>.</p>
  
  <p>Thanks! :)</p>