您的位置:首页 > 其它

运行时中函数调用黑魔法swizzle,交换两个方法或者改变一个方法的实现

2016-08-01 13:57 543 查看
类方法的实现的改变
+ (IMP)swizzleClassSelector:(SEL)origSelector
withIMP:(IMP)newIMP
{
Class class = [self class];
Method origMethod = class_getClassMethod(class,
origSelector);
IMP origIMP = method_getImplementation(origMethod);
if(!class_addMethod(self, origSelector, newIMP,
method_getTypeEncoding(origMethod)))
{
method_setImplementation(origMethod, newIMP);
}

return origIMP;
}


实例方法的实现的改变
+ (IMP)swizzleSelector:(SEL)origSelector
withIMP:(IMP)newIMP {
Class class = [self class];
Method origMethod = class_getInstanceMethod(class,
origSelector);
IMP origIMP = method_getImplementation(origMethod);
if(!class_addMethod(self, origSelector, newIMP,
method_getTypeEncoding(origMethod)))
{
method_setImplementation(origMethod, newIMP);
}

return origIMP;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐