tags:

views:

134

answers:

1

I`m newbie in objective-c and I have some questions:

  1. What is an IMP?

  2. What is msgSend function?

+1  A: 

The IMP is an IMplementation Pointer, which is basically the hook that determines what gets run upon receipt of a message (like foo length). You don't usually need them unless you're getting down and dirty; it's usually easier to deal with selectors.

6.1 What is an IMP ?

   It's the C type of a method implementation pointer, a function

pointer to the function that implements an Objective-C method. It is defined to return id and takes two hidden arguments, self and _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)];

From section 6.1 of the Objective C FAQ.

As for the msgSend, that's the way you invoke a remote message on another object; objc_msgSend(foo,@selector(bar)) is roughly the same as [foo bar]. But these are all low-level implementation details; you rarely (if ever) need to use the expanded calls for Objective C code, since you can do @selector to get hold of a method and performSelector: to invoke it on any object.

AlBlue