Re: noob question regarding proxy object returned by -selection method of NSArrayController
Re: noob question regarding proxy object returned by -selection method of NSArrayController
- Subject: Re: noob question regarding proxy object returned by -selection method of NSArrayController
- From: Stuart Malin <email@hidden>
- Date: Sun, 29 Jun 2008 08:47:55 -0700
On Jun 29, 2008, at 12:07 AM, Ken Thomases wrote:
On Jun 28, 2008, at 12:43 PM, Stuart Malin wrote:
I have a button in the GUI that should cause various changes to
the person selected in the table. In the method that is the target
of the button's action, I need to get the selected Person object
so I can operate on it.
I know I could use the -selection method of the NSController to
get a proxy object, and then use -valueForKey: and -setValueForKey
to operate on the object via the proxy. However, this puts the
logic of the manipulations in my appController. I'd rather the
collection of manipulations be in the Person class, (i.e., have
instance methods in the Person class that update a person object).
If I do this (have the update logic in the Person class), then I
can't use the proxy object returned by the -selection method of
the NSController (because the proxy object doesn't respond to the
methods of the backing class).
Have you considered binding the button's target to the array
controller's selection, and putting the action methods on the
Person directly?
Yes, I see that can do that. My problem is hypothetical -- just
"test" code to get a working understanding of the dynamics.
(You might need to use a model key path of "self" to get around the
proxy-ness.)
Yes -- Owen Yamauchi pointed out to me the use of self (though not as
a part of a model key path).
On Jun 28, 2008, at 11:14 AM, Owen Yamauchi wrote:
How about [[controller selection] valueForKey:@"self"]? NSObject
has a
-self method which just returns the receiver, and since the proxy
object must respond to the KVC query as if it were the underlying
object, you get the underlying object back.
I found this quite intriguing -- and wondered why the -self method
worked when I couldn't invoke the instance method I'd made. So I
tried Owen's approach, substituting my instance method (that adjusts
several of the object's ivars) for "self " in invoking -valueForKey
on the proxy object -- lo and behold, it worked. I guess this
shouldn't really have surprised me -- after all, how can the KVC
mechanism know if a method is a canonical accessor (rhetorically asked).
So long as no arguments need to be passed to the instance method, one
can use -valueForKey on the proxy object to execute an arbitrary
instance method (i.e., not a canonical getter accessor). But doing
this seems "wrong" to me -- using the KVC mechanism in a way that it
isn't intended. I suspect I'll avoid using it, but am curious if
this (using KVC to invoke non-canonical getter) is troublesome or not?
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden