Objective-C中的_cmd是什麼意思?

Objective-C的編譯器在編譯後會在每一個方法中加兩個隱藏的參數:指針

  • 一個是_cmd,當前方法的一個SEL指針。
  • 另外一個就是用的比較多的self,指向當前對象的一個指針。

在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
相關文章
相關標籤/搜索