Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to ask a method for its name?

I'm trying to debug an iPhone app I'm working on, and the idea of adding fifty NSLog statements to the various source files gives me the willies.

What I'd like to do is write a pair of statements, say

NSString *methodName = [self methodName];
NSLog(@"%@", methodName);

that I can just paste into each method I need to. Is there a way to do this? Is there some Objective-C construct for asking a method for its name? Or am I gonna have to do this the hard way?

like image 284
Andy Avatar asked Apr 22 '10 02:04

Andy


2 Answers

Try NSLog(@"%s", __func__). This prints out a pretty description, like -[MyView drawRect:].

This also works with functions. It's a compiler feature.

like image 110
Ken Avatar answered Sep 28 '22 10:09

Ken


Use: NSLog("%@", NSStringFromSelector(_cmd));

_cmd is a special variable passed to every method just like self which is a reference to the selector that caused the method to be invoked (basically the method's name and signature).

like image 32
Jason Coco Avatar answered Sep 28 '22 10:09

Jason Coco