IMP implementation pointer
suggest changeIMP 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.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents