Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically retrieving current method's name

Tags:

objective-c

I'd like to be able to put trace messages of the current class name and method name for all classes in my project.

Is there a way to get the current method's name at run time, similar to:

NSLog(@"classname: %@", [self className]);

where self is a class derived out of NSObject?

like image 871
Alexi Groove Avatar asked Sep 22 '09 00:09

Alexi Groove


People also ask

How do you find the current method name?

The current method name that contains the execution point that is represented by the current stack trace element is provided by the java. lang. StackTraceElement. getMethodName() method.

Which among the following helps in fetching the name of the class that is currently executed?

By Object Class : We can use Class. getEnclosingMethod(), this method returns a Method object representing the instantly enclosing method of the prime class.


1 Answers

NSLog(@"method name: %@", NSStringFromSelector(_cmd));

_cmd is a hidden argument (like self) that all Objective-C methods receive. Its value is the selector that was used to call the method.

like image 86
Chuck Avatar answered Sep 18 '22 11:09

Chuck