in objective-c can I access the calling classnam开发者_运维知识库e/methodname within a method?
Background:
I am currently putting the following line in some of my methods for logging:
NSLog(@"<%@:%@:%d>", NSStringFromClass([self class]), NSStringFromSelector(_cmd), __LINE__);
It would be nice to also include the class name and method name(location) of the code that called the method.
Is this possible? What would the code be for this?
thanks
There's no practical way to do this (hence all the methods in Cocoa with sender
arguments). There isn't even a guarantee that there is such a thing — it could be called from a plain old, self
-less function, or it could have been called from a method that was inlined into its calling method, or we could have got to that line from a jump, or….
If you are just doing this in the context of a debugging executable, consider trying the GDB Backtrace function:
http://www.gnu.org/software/libc/manual/html_node/Backtraces.html
Not sure what that will end up looking like for Objective-C, but if you go back up the chain long enough you should find something invoking a message...
Or try reading through this and see if it helps at all:
Why does backtrace not contain Objective-C symbols regardless of -rdynamic?
http://iphoneincubator.com/blog/debugging/the-evolution-of-a-replacement-for-nslog
little googling can go long way :)
I use PRETTY_FUNCTION and DLog.
精彩评论