Runtime中的method_exchangeImplemen

2019-07-18  本文已影响0人  灬朴一生

前言

最近一直在整理原写过的一些工作方法,慢慢发一些吧,大佬们如果有更好的方法和建议请多指点

主要是围绕runtime中的method_exchangeImplementations方法交换做了两个小例子

Build Settings -> Enable Strict Checking of objc_msgSend Calls -> No

// 拦截 实例方法并替换

//获取系统方法地址
Method sytemMethod = class_getInstanceMethod([NSBundle class], @selector(bundleIdentifier));
//获取自定义方法地址
Method customMethod = class_getInstanceMethod([NSBundle class], @selector(newBundleIdentifier));
//交换两个方法的实现
method_exchangeImplementations(sytemMethod, customMethod);
// 拦截 类方法并替换

//获取系统方法地址
Method sytemMethod = class_getClassMethod([UIFont class], @selector(systemFontOfSize:));
//获取自定义方法地址
Method customMethod = class_getClassMethod([UIFont class], @selector(newSystemFontOfSize:));
//交换两个方法的实现
method_exchangeImplementations(sytemMethod, customMethod);

主要是根据自己的项目需求修改了UIFont类与NSBundld

上一篇 下一篇

猜你喜欢

热点阅读