ios runtime 动态向类添加方法

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

ios runtime 动态向类添加方法

上一篇:gdbserver 安卓apk


下一篇:UVa 11111 一般Matrioshka 及 scanf 处理一行