Re: Abusing targetForAction: with non-action selectors
Re: Abusing targetForAction: with non-action selectors
- Subject: Re: Abusing targetForAction: with non-action selectors
- From: "Michael Ash" <email@hidden>
- Date: Tue, 16 Sep 2008 15:09:27 -0400
On Mon, Sep 15, 2008 at 7:49 PM, Charles Srstka
<email@hidden> wrote:
> On Sep 15, 2008, at 3:13 PM, Dave Dribin wrote:
>
>> Hello,
>>
>> Is it safe to use -[NSApplication targetForAction:] with non-action
>> selectors? For example, selectors that have more than one argument, non-id
>> first argument, or return values?
>>
>> The documentation for targetForAction:to:from: seems to imply it just does
>> a respondsToSelector: on each object in the responder chain, and it seems to
>> behave this way in my tests. I'd just like to make sure it is safe to rely
>> on this behavior, or if we should find some other approach.
>
> I'd say it's not safe, since the documentation doesn't make any promises
> about working correctly with a non-action selector, and thus your code might
> break with some future OS version. It wouldn't be that hard to write your
> own method that would just do a respondsToSelector: on each object in the
> responder chain, and then you'd know *exactly* what the method is doing.
> This is what I'd recommend.
While -targetForAction: explicitly mentions an action selector,
-targetForAction:to:from: has documentation which makes it pretty
clear that it works with any selector, as it has no mention of being
restricted to only action selectors. Since you get the same behavior
as -targetForAction: if you pass nil for "to" and anything you want
for "from", I would just switch the code to use
-targetForAction:to:from: and consider it safe. While it's true that
the method name would indicate that it's for action selectors, the
explicit API contract in the form of the method's documentation makes
no such restriction.
Mike
_______________________________________________
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