IOS 获取当前方法的方法名

来源:互联网 发布:室内采光优化 编辑:程序博客网 时间:2024/05/16 04:14

之前一个项目做统计功能的时候,需求不止统计界面,而且还要求统计很多具体事件,当时完全逐个填写字符串来做的。现在想想应该可以简单一点,比如我在官方文档上看到的_cmd方法。


“The _cmd variable is a hidden argument passed to every method that is the current selector”
就是说_cmd在Objective-C的方法中表示当前方法的selector

比如我们打印要调用的方法可以这样写:

- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

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

}

打印结果:

Current method: viewDidLoad


统计的字符串完全可以用

[NSStringstringWithFormat:@"%@%@",[selfclass],NSStringFromSelector(_cmd)]

来代替手写





不得不说CSDN真的不行,偶尔登录不了,图经常被吃,再出问题真的要换地方了

2 0
原创粉丝点击