2
A
回答
3
IMP是一個IMplementation指針,它基本上是決定接收消息(如foo長度)時運行的鉤子。除非你越來越骯髒,否則你通常不需要它們;處理選擇器通常更容易。
6.1什麼是IMP?
It's the C type of a method implementation pointer, a function
指針 到實現一個Objective-C方法的功能。它被定義 返回ID,並採取兩個隱藏參數,自我和_cmd:
typedef id (*IMP)(id self,SEL _cmd,...); 6.2 How do I get an IMP given a SEL ? This can be done by sending a methodFor: message : IMP myImp = [myObject methodFor:mySel]; 6.3 How do I send a message given an IMP ? By dereferencing the function pointer. The following are all equivalent : [myObject myMessage]; or IMP myImp = [myObject methodFor:@selector(myMessage)]; myImp(myObject,@selector(myMessage)); or [myObject perform:@selector(myMessage)];
從Objective C FAQ的第6.1節。
至於msgSend,這就是你調用另一個對象的遠程信息的方式; objc_msgSend(foo,@ selector(bar))與[foo bar]大致相同。但是這些都是低級的實施細節;您很少(如果曾經)需要使用Objective C代碼的擴展調用,因爲您可以執行@selector來獲取方法和執行選擇器:在任何對象上調用它。