Cocoa 绑定的 NSHandlesContentAsCompoundValueBindingOption 到底做了什么?

发布于 2024-11-10 10:38:28 字数 497 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没企图 2024-11-17 10:38:28

这里很好地解释了消息流: http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CocoaBindings/Concepts/MessageFlow.html#//apple_ref/doc/uid/TP40002149-186285

这是我尝试回答:

  1. contentObjectcontentArray 指定的原始内容对象或使用 valueForKeyPath: 从 NSUserDefaultsController 检索 contentSet 绑定
  2. 该内容对象使用值转换器的 transformedValue: 方法进行转换
  3. 来自用户的新值是插入到转换后的内容对象中
  4. 使用inverseTransformedValue:对内容对象进行逆变
  5. 换新的逆变换内容对象被设置为新的内容对象并使用以下方法传递给NSUserDefaultsController: 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:

  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:
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文