如何在 Objective-C 中记录每个被调用的类方法的名称? [英] How can I log names of each called class method in Objective-C?
问题描述
当我想查看对象方法调用的顺序时,我必须像这样记录我实现的每个方法.
When I want to see the order of the object method calls, I have to put logs each method I implemented like this.
- (void)updateTime:(float)time
{
NSLog(@"%s", __PRETTY_FUNCTION__);
所以我不得不把这段代码放在类的每个方法中,每次调试一个类的时候插入和删除这么多日志函数调用很无聊.
Hence I have to put this code in every method on the class, and it's very boring to insert and delete so many log function calls every time I am debugging a class.
那么如何在每个方法调用的类中触发 NSLog(@"%s", __PRETTY_FUNCTION__);
?
So how can I trigger NSLog(@"%s", __PRETTY_FUNCTION__);
in a class on each method call?
我最终得到了这段代码.并且在其他任何地方都没有得到答复.
I ended up with this code. And was not answered anywhere else.
- (BOOL)respondsToSelector:(SEL)aSelector {
if(aSelector){
NSLog(@"%@", NSStringFromSelector(aSelector));
}
return [super respondsToSelector:aSelector];
}
推荐答案
您可以使用 NSObjCMessageLoggingEnabled=YES
运行您的可执行文件.您可以在 Xcode Schemes 中的 Run 下的 Argument Variables 下进行设置.
You can run your executable with NSObjCMessageLoggingEnabled=YES
. You can set this in Xcode Schemes, under Argument Variables under Run.
你最终会得到类似的输出
You end up with output like
+ NSObject NSObject initialize
+ NSNotificationCenter NSObject initialize
+ NSNotificationCenter NSNotificationCenter defaultCenter
更多信息这里和
这篇关于如何在 Objective-C 中记录每个被调用的类方法的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!