ios runtime 动态向类添加方法
时间:2014-05-13 08:09:05
收藏:0
阅读:412
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];
评论(0)