Re: How to get the name of a method at runtime?
Re: How to get the name of a method at runtime?
- Subject: Re: How to get the name of a method at runtime?
- From: "Sherm Pendley" <email@hidden>
- Date: Sun, 9 Mar 2008 05:14:19 -0400
On Sun, Mar 9, 2008 at 4:44 AM, Stuart Malin <email@hidden> wrote:
> I'd like to have a method determine the name of the method that
> invoked it -- as an NSString.
>
> For example
>
> - (void) method1 {
> [someObject method2];
> }
>
> - (void) method2 {
> // here, I'd like to be able to find the name of the caller
> // in this example, that would be "method1"
> }
Seems to me the simplest thing to do would be to pass _cmd as an argument,
much like the caller of an IBAction method passes self:
- (void) method1 {
[someObject method2:_cmd];
}
- (void) method2: (SEL)callerSel {
NSLog(@"Caller's selector: %@", NSStringFromSelector(callerSel));
}
I have a suspicion that this is either trivial, and I'm just missing
> the obvious, or its far from trivial and requires accessing the stack and
> some
> symbol table.
I think maybe you missed the existence of _cmd. Both self (this object) and
_cmd (this selector) are passed as implicit arguments to every Objective-C
method. Knowing that, forwarding them to another method does indeed become
rather trivial. :-)
sherm--
>
>
> Any pointers (no pun intended) as to where I can find more
> information to understand the internal mechanisms would be appreciated.
>
>
> _______________________________________________
>
> 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
>
_______________________________________________
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