Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dynamic Resolution Of Instance Method
#import <Foundation/Foundation.h>
void dynamicMethodIMP(id self, SEL _cmd) {
NSLog(@"This Method(%@) is dynamically resolved.", NSStringFromSelector(_cmd));
}
@interface MyObject : NSObject
{
int i;
}
@end
@implementation MyObject
+(BOOL)resolveInstanceMethod:(SEL)aSEL
{
if(aSEL == @selector(resolveThisMethodDynamically)) {
class_addMethod([self class], aSEL, (IMP) dynamicMethodIMP, "v@:");
return YES;
}
return [super resolveInstanceMethod:aSEL];
}
@end
int main(int argc, char const *argv[])
{
@autoreleasepool{
MyObject *m = [[MyObject alloc] init];
[m resolveThisMethodDynamically];
[m release];
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment