1、定义C函数:
void dynamicMethodIMP(id self, SEL _cmd)
{
NSLog(@"蜗牛也疯狂");
}
2、重写函数+(BOOL)resolveInstanceMethod:(SEL)sel
+(BOOL)resolveInstanceMethod:(SEL)sel
{
class_addMethod([self class], sel, (IMP)dynamicMethodIMP, "v@:");
return [super resolveInstanceMethod:sel];
}
3、使用:
SEL select = NSSelectorFromString(@"resolveThisMethodDynamically");
[class resolveInstanceMethod:select];
[[class instance] performSelector:select];
ios runtime 动态向类添加方法,布布扣,bubuko.com
原文地址:http://blog.csdn.net/wnyfk/article/details/25623439