What does Cocoa binding's NSHandlesContentAsCompoundValueBindingOption do exactly?
When binding an NSArrayController
's contentArray to an NSUserDefaultsController
, you have to check the "Handles Content As Compound Value" checkbox on the binding. This has become conventional wisdom, but what does the option actually do?
I wrote a small test app and could observe that with the option turned on, the whole contentArray
is passed to the binding source's setValue:forKey:
whenever you edit a property of an element in the array. When the option is off, only the element object itself is modified and the binding source is not notified.
This explains why the option is needed to make NSUserDefaultsContr开发者_C百科oller
work (otherwise it wouldn't notice that you had edited something in the array and never save the change). But it doesn't explain who is doing what differently exactly. Is the array controller taking charge of this option and writing back the content array when it observes a change? If so, how does it relate to the stated purpose of the option which is to "use a reversible value transformer to translate [...] compound values temporarily into smaller pieces"?
The message flow is explained here pretty well: http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CocoaBindings/Concepts/MessageFlow.html#//apple_ref/doc/uid/TP40002149-186285
Here's my attempt to answer:
- The original content object specified by the
contentObject
,contentArray
orcontentSet
binding is retrieved from the NSUserDefaultsController usingvalueForKeyPath:
- That content object is transformed using the value transformer's
transformedValue:
method - The new value from the user is inserted into the transformed content object
- The content object is inverse transformed using
inverseTransformedValue:
- The new, inverse transformed content object is set as the new content object and passed to the NSUserDefaultsController using
setValue:forKeyPath:
精彩评论