IMP implementation pointer

suggest change

IMP is a C type referring to the implementation of a method, also known as an implementation pointer. It is a pointer to the start of a method implementation.

Syntax:

id (*IMP)(id, SEL, …)

IMP is defined by:

typedef id (*IMP)(id self,SEL _cmd,…);

To access this IMP, the message “methodForSelector” can be used.

Example 1:

IMP ImpDoSomething = [myObject methodForSelector:@selector(doSomething)];

The method addressed by the IMP can be called by dereferencing the IMP.

ImpDoSomething(myObject, @selector(doSomething));

So these calls are equal:

ImpDoSomething(myObject, @selector(doSomething));
[myObject doSomething];
[myObject performSelector:@selector(doSomething)];
[myObject performSelector:NSSelectorFromString(@"doSomething")];

Example :2:

SEL otherWaySelector = NSSelectorFromString(@"methodWithFirst:andSecond:andThird:");

IMP methodImplementation = [self methodForSelector:otherWaySelector];

id result = methodImplementation(self,
                          otherWaySelector,
                          first,
                          second,
                          third);

NSLog(@"methodForSelector : %@", result);

methodForSelector: returns a pointer to the C function that implements the method, which can then be called directly.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents