2009-07-28 54 views

回答

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来获取方法和执行选择器:在任何对象上调用它。