7

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.

Roger
  • 4,737
  • 4
  • 43
  • 68

1 Answers1

8

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.

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • 13
    Actually, you can also bypass the proxy by getting its `self` value: `[[myArrayController selection] valueForKey:@"self"]`. Note that you have to use KVC to do this, calling the `self` method will return the proxy. – ughoavgfhw Oct 10 '11 at 17:51
  • @ughoavgfhw This was a huge mystery and headache for me... With that out of the way, I immediately become curious about your username. – Mazyod Oct 28 '13 at 03:30