Re: Current Selector
Re: Current Selector
- Subject: Re: Current Selector
- From: Bill Bumgarner <email@hidden>
- Date: Tue, 7 Jan 2003 08:46:29 -0500
On Monday, Jan 6, 2003, at 17:45 US/Eastern,
email@hidden wrote:
Is there a way, programmatically, to get the name of the current
selector (method) that is executing? I'm trying to log some debugging
info, and I'd like to be able to indicate the selector without having
to write a custom like of code for each method.
Just consider an ObjC method as a C function for a moment. These
methods....
- (void) doSomething: sender;
- (BOOL) isItTrueWithThis: aString andThat: (int) aCount;
... would be declared as ...
void doSomething(id self, SEL _cmd);
BOOL isItTrueWithThisAndThat(id self, SEL _CMD, id aString, int aCount);
Just like 'self' is really an argument to the method, there is a second
argument called '_cmd' that contains the selector for the method.
Historically, Obj-C was originally implemented as a C preprocessor that
would turn every method call, say [self doSomething: sender], into a
call to objc_msgSend() -- literally, objc_msgSend(self,
@selector(doSomething:), sender.
To answer your question, you simply need to turn _cmd into a useful
string; NSStringFromSelector() does the trick. There are other
useful bits to grab, as well. For both exceptions and debugging
purposes, I often do something like:
NSLog(@"%s:%d [%@ -%@] .... your error message here ....", __FILE__,
__LINE__, NSStringFromClass([self class]), NSStringFromSelector(_cmd),
... rest of your arguments here...);
b.bum
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.