Is there a preprocessor macro that expands to the current selector?

825 views Asked by At

Possible Duplicate:
Dynamically retrieving current method's name
Obj-C introspection: How can a method reference it's own selector?

This applies to Objective-C, is there a preprocessor macro or something to get the SEL value of the current selector? Specifically I'm looking for something like:

-(void) someSelector
{
    SEL mySelector = __CURRENT_SELECTOR__;
    NSLog(@"I'm in selector %@",NSStringFromSelector(mySelector));
}

it's kinda like the __FILE__ macro but this to obtain the current selector. Pretty useful to pass it to others while not worrying to update it if the selector name is changed.

3

There are 3 answers

0
sidyll On BEST ANSWER

Every method has two implicit parameters, self which is an id (the receiver) and a SEL called _cmd, which is probably what you want.

Note that this has nothing to do with preprocessor or anything before compile-time, _cmd is not a macro, it's an argument.

-(void) someSelector
{
    NSLog(@"I'm in selector %@",NSStringFromSelector(_cmd));
}
0
Shubhank On
NSLog( @"%s" , _cmd );

_cmd will get you the current selector(only available in objective-c)

0
mohsenr On

What sidyll said should answer your question. Just wanted to add if you just need it for logging you can also use the usual C keywords, e.g.

NSLog( @"%s" , __PRETTY_FUNCTION__ );