IOS 获取当前方法的方法名
2017-04-06 13:55
92 查看
之前一个项目做统计功能的时候,需求不止统计界面,而且还要求统计很多具体事件,当时完全逐个填写字符串来做的。现在想想应该可以简单一点,比如我在官方文档上看到的_cmd方法。
“The _cmd variable is a hidden argument passed to every method that is the current selector”
就是说_cmd在Objective-C的方法中表示当前方法的selector
比如我们打印要调用的方法可以这样写:
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(@"Current method: %@",NSStringFromSelector(_cmd));
}
打印结果:
Current method: viewDidLoad
统计的字符串完全可以用
[NSString
stringWithFormat:@"%@:%@",[self
class],NSStringFromSelector(_cmd)]
来代替手写
不得不说CSDN真的不行,偶尔登录不了,图经常被吃,再出问题真的要换地方了
“The _cmd variable is a hidden argument passed to every method that is the current selector”
就是说_cmd在Objective-C的方法中表示当前方法的selector
比如我们打印要调用的方法可以这样写:
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(@"Current method: %@",NSStringFromSelector(_cmd));
}
打印结果:
Current method: viewDidLoad
统计的字符串完全可以用
[NSString
stringWithFormat:@"%@:%@",[self
class],NSStringFromSelector(_cmd)]
来代替手写
不得不说CSDN真的不行,偶尔登录不了,图经常被吃,再出问题真的要换地方了
相关文章推荐
- iOS UIFont获取当前系统字体大小的方法
- IOS获取当前位置坐标不执行代理方法
- iOS 获取当前设备连接Wi-Fi的方法总结
- ios-day11-01(UIWindow的常用方法。makeKeyWindow、makeKeyAndVisible、获取当前应用的主窗口和所有窗口)
- iOS获取当前时间和当前时间戳的方法
- iOS 获取当前的ViewController的方法
- iOS获取当前网络环境的实现方法(推荐)
- iOS获取当前版本号 Bundle Id等信息的方法
- ios 获取当前控制器的方法
- iOS获取当前屏幕控制器的方法
- cocos2d-x在win32和iOS、android下获取当前系统时间的方法
- iOS 两种方法获取当前网络状态
- iOS获取当前设备WiFi信息的方法
- iOS如何获取当前View所在控制器的方法
- IOS 获取当前方法的方法名
- iOS 获取当前网络状态的两种方法
- ios-day11-01(UIWindow的常用方法。makeKeyWindow、makeKeyAndVisible、获取当前应用的主窗口和所有窗口)
- iOS获取当前版本号 Bundle ID等信息的方法
- IOS获取当前经纬度 有时不调用代理方法
- iOS获取当前连接的WiFi名称即SSID的方法