Search code examples
cocoansarraycontroller

Programmatically retrieving the selected object of an NSArrayController


When I use an NSArrayController with instances of NSManagedObject, I was under the impression that the following would give me its selected object:

[myArrayController selection]

However, this gives me an instance of some private NS Proxy class.

When I use:

[[myArrayController selectedObjects] objectAt: 0]

...all is fine and I have my instance of NSManagedObject.

I understand the necessity for these methods as you can probably have lists which allow for multiple selection. I do not understand why selection does not give me the instance of NSManagedObject.


Solution

  • It states in the documentation for NSObjectController (which NSArrayController inherits from) that this method returns a proxy object.

    You will be able to use any KVC methods on the proxy object, but if you want any class - specific functionality you will have to use the second method in your sample above. As you've stated, this is to return a valid object in cases of no or multiple selection.