What does Cocoa binding's NSHandlesContentAsCompoundValueBindingOption do exactly?

怎甘沉沦 提交于 2019-12-23 09:09:12

问题


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 NSUserDefaultsController 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"?


回答1:


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:

  1. The original content object specified by the contentObject, contentArray or contentSet binding is retrieved from the NSUserDefaultsController using valueForKeyPath:
  2. That content object is transformed using the value transformer's transformedValue: method
  3. The new value from the user is inserted into the transformed content object
  4. The content object is inverse transformed using inverseTransformedValue:
  5. The new, inverse transformed content object is set as the new content object and passed to the NSUserDefaultsController using setValue:forKeyPath:


来源:https://stackoverflow.com/questions/6183026/what-does-cocoa-bindings-nshandlescontentascompoundvaluebindingoption-do-exactl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!