简体   繁体   English

为什么-[NSObjectController selection]返回代理对象?

[英]Why does -[NSObjectController selection] return a proxy object?

NSObjectController and its subclasses return a proxy object for the selection. NSObjectController及其子类返回用于选择的代理对象。 I'd like to understand why they chose to design it this way. 我想了解他们为什么选择这种方式进行设计。 The documentation doesn't seem to shed much light on why this is. 该文档似乎并没有阐明为什么会这样。

Apple's Cocoa Bindings Programming Topics has this to say: Apple的Cocoa Bindings编程主题具有这样的说法:

You can establish bindings to a controller's selection method or the selectedObjects method. 您可以建立到控制器的选择方法或selectedObjects方法的绑定。 However, you should avoid binding through the selectedObjects array, for example selectedObjects.name. 但是,应避免通过selectedObjects数组进行绑定,例如selectedObjects.name。 Instead, you should use selection.name. 相反,您应该使用selection.name。 Similarly, you should avoid observing keys through the array returned by selectedObjects. 同样,您应该避免通过selectedObjects返回的数组观察键。 The proxy returned by selection is more efficient at managing changes in key-value observing as the selection changes. 通过选择返回的代理在管理随选择更改的键值观察中的更改方面更有效。

I think you'll find that your answer lies in that final sentence. 我认为您会发现答案就在最后一句话中。

For what it's worth, [objectController valueForKeyPath:@"selection.self"] returns the selected object itself. 对于它的价值, [objectController valueForKeyPath:@"selection.self"]返回所选对象本身。

Also : 另外

Often you need to directly access the objects currently selected by the controller, rather than the proxy object returned by selection. 通常,您通常需要直接访问控制器当前选择的对象,而不是选择返回的代理对象。 NSObjectController and its subclasses provide the selectedObjects method to allow you to do just that. NSObjectController及其子类提供了selectedObjects方法,使您可以做到这一点。 This method returns an array containing the objects that are currently selected by the receiver. 此方法返回一个包含接收方当前选择的对象的数组。 NSObjectController's implementation returns an array containing a single object, the content object. NSObjectController的实现返回一个包含单个对象(内容对象)的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM