Objective-C的编译器在编译后会在每一个方法中加两个隐藏的参数:指针
在Apple的官方介绍里看到轻描淡写的说了一句:“The _cmd variable is a hidden argument passed to every method that is the current selector”,其实说的就是_cmd在Objective-C的方法中表示当前方法的selector,正如同self表示当前方法调用的对象实例同样。code
好比,咱们要打印当前要调用的方法,能够这样来写:对象
- (void)viewDidLoad { [super viewDidLoad]; NSLog(@"Current method: %@ %@",[self class],NSStringFromSelector(_cmd)); }
输出结果以下:cmd
TestingProject[570:11303] Current method: FirstViewController viewDidLoad