Last active
May 3, 2018 08:31
-
-
Save zhangkn/9a8464db30e597d9ebb5291070b403f1 to your computer and use it in GitHub Desktop.
Dynamic Method Resolution;动态添加方法:动态添加方法就是在消息转发前在`+ (BOOL)resolveInstanceMethod:(SEL)sel`方法中使用`class_addMethod()` 添加方法。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void dynamicMethodIMP(id self, SEL _cmd) { | |
// implementation .... | |
printf("执行了dynamicMethodIMP!!!!"); | |
} | |
//you can dynamically add it to a class as a method (called resolveThisMethodDynamically) using resolveInstanceMethod: like this: | |
@implementation MyClass | |
+ (BOOL)resolveInstanceMethod:(SEL)aSEL | |
{ | |
if (aSEL == @selector(resolveThisMethodDynamically)) { | |
class_addMethod([self class], aSEL, (IMP) dynamicMethodIMP, "v@:"); | |
return YES; | |
} | |
return [super resolveInstanceMethod:aSEL]; | |
} | |
@end | |
//performSelector:@selector(resolveThisMethodDynamically)];//输出:dynamicMethodIMP!!!! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment