SEL original = @selector(imageNamed:);
SEL swizzle = @selector(sy_imageNamed:);
/// 需要用objc_getMetaClass,直接用[self class]无效
Class class = objc_getMetaClass(object_getClassName(self));
Method originalMethod = class_getClassMethod(class, @selector(imageNamed:));
Method swizzlingMethod = class_getClassMethod(class, @selector(sy_imageNamed:));
BOOL didAddMethod = class_addMethod(class, original, method_getImplementation(swizzlingMethod), method_getTypeEncoding(swizzlingMethod));
if (didAddMethod) {
class_replaceMethod(class, swizzle, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, swizzlingMethod);
}
iOS 交换系统类方法
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 需求: 每次UIImage加载图片,告诉我是否加载成功 当系统提供的控件不能满足我们的需求的时候,我们...
- 导入<objc/runtime.h> 1. 获得某个类的类方法 Method m1 = class_getClas...
- Method Swizzling 是什么 Method Swizzling是objective-c中的黑魔法,算是...
- 我们之前说到,想要统计每个页面的启动时间。我们可以知道,每个页面都继承了UIViewController,假设每个...
- 现在一段时间在回顾小runtime 的一些知识点。搜了一些资料,自己学习后总结下,以便后面回顾。 Method S...