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: Ken Thomases <email@hidden>
- Date: Sun, 29 Jun 2008 11:04:20 -0500
On Jun 29, 2008, at 10:47 AM, Stuart Malin wrote:
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).
You might get in trouble if its return type is void. Who knows what -
valueForKey: will do in that case?
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?
Well, it is certainly troublesome from the point of view of code
clarity and comprehension.
From the point of view of what will happen, it's fairly well defined,
so I don't think you're setting yourself up for the code to blow up.
That is, the fact that it's working in your test is not just an
unreliable fluke.
Still, your gut reaction is one I share. Don't do it.
However, note that -self is in fact a perfectly fine getter. I have
no qualms about using that particular key via KVC.
Cheers,
Ken
_______________________________________________
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