Cocoa 绑定的 NSHandlesContentAsCompoundValueBindingOption 到底做了什么?
将 NSArrayController
的 contentArray 绑定到 NSUserDefaultsController
时,您必须选中绑定上的“将内容处理为复合值”复选框。这已经成为传统观念,但这个选项实际上有什么作用呢?
我编写了一个小型测试应用程序,可以观察到,打开该选项后,每当您编辑某个属性的属性时,整个 contentArray
都会传递到绑定源的 setValue:forKey:
数组中的元素。当该选项关闭时,仅修改元素对象本身,并且不会通知绑定源。
这解释了为什么需要该选项来使 NSUserDefaultsController
工作(否则它不会注意到您在数组中编辑了某些内容并且永远不会保存更改)。但它并不能准确地解释谁在做什么不同的事情。数组控制器是否负责此选项并在观察到更改时写回内容数组?如果是这样,它与该选项的既定目的“使用可逆值转换器将[...]复合值暂时转换为较小的部分”有何关系?
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"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里很好地解释了消息流: http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CocoaBindings/Concepts/MessageFlow.html#//apple_ref/doc/uid/TP40002149-186285
这是我尝试回答:
contentObject
、contentArray
指定的原始内容对象或使用valueForKeyPath:
从 NSUserDefaultsController 检索contentSet
绑定transformedValue:
方法进行转换inverseTransformedValue:
对内容对象进行逆变setValue:forKeyPath:
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:
contentObject
,contentArray
orcontentSet
binding is retrieved from the NSUserDefaultsController usingvalueForKeyPath:
transformedValue:
methodinverseTransformedValue:
setValue:forKeyPath: