简体   繁体   中英

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

NSObjectController and its subclasses return a proxy object for the selection. 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:

You can establish bindings to a controller's selection method or the selectedObjects method. However, you should avoid binding through the selectedObjects array, for example selectedObjects.name. Instead, you should use selection.name. Similarly, you should avoid observing keys through the array returned by 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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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