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];


ios runtime 动态向类添加方法,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!