Every method call also passes two hidden arguments: an id
named self
and a SEL
named _cmd
. You can use NSStringFromSelector
to convert the method selector to an NSString:
NSStringFromSelector(_cmd);
Use __func__
. This is a C string, so for an NSString, use [NSString stringWithUTF8String:__func__]
.
This has two advantages over _cmd
:
__func__
is required to exist by C99.)"-[MyView drawRect:]"
.As an example of where this sort of thing is useful: This is a template for NSLog messages that I use:
NSLog(@"%@ %@: %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), @"A Message");
This dumps the class and the method to the console when logging.
As per Martin's answer, but you might also like to read the Objective C 2.0 Runtime information.
Playing in the guts like this tends to lead to hard to manage code, however.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With