Programmatically retrieving the selected object of an NSArrayController
Asked Answered
A

1

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.

Accompaniment answered 10/10, 2011 at 16:33 Comment(0)
I
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.

Icsh answered 10/10, 2011 at 17:47 Comment(2)
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.Pilkington
@Pilkington This was a huge mystery and headache for me... With that out of the way, I immediately become curious about your username.Shanteshantee

© 2022 - 2024 — McMap. All rights reserved.