#import "UIViewController+Swizzling.h"
#import <objc/runtime.h>
@implementation UIViewController (Swizzling)
+(void)load{
//虽然load只执行一次,但是为了保险起见,我们还是给加个dispatch_once吧,良好的编程习惯,从这里开始
static dispatch_once_t token;
dispatch_once(&token, ^{
SEL orginSel = @selector(viewWillAppear:);
SEL overrideSel = @selector(overrideViewWillAppear:);
Method originMethod = class_getInstanceMethod([self class], orginSel);
Method overrideMethod = class_getInstanceMethod([self class], overrideSel);
//原来的类没有实现指定的方法,那么我们就得先做判断,把方法添加进去,然后进行替换
if (class_addMethod([self class], orginSel, method_getImplementation(overrideMethod) , method_getTypeEncoding(originMethod))) {
class_replaceMethod([self class],
overrideSel,
method_getImplementation(originMethod),
method_getTypeEncoding(originMethod));
}else{
//交换实现
method_exchangeImplementations(originMethod, overrideMethod);
}
});
}
- (void)overrideViewWillAppear:(BOOL)animation{
NSLog(@"%@-----overrideViewWillAppear",self);
//这里并不会造成死循环,因为这个时候是去调用原来的ViewWillAppear:(BOOL)animation方法了。
[self overrideViewWillAppear:animation];
}
@end
1.以上是角交换两个方法可以做app全埋点
#import "NSMutableDictionary+Swizzling.h"
#import <objc/runtime.h>
@implementation NSMutableDictionary (Swizzling)
+ (void)load
{
static dispatch_once_t token;
dispatch_once(&token, ^{
SEL orginSel = @selector(setObject:forKey:);
SEL overrideSel = @selector(overrideSetObject:forKey:);
Class o_class = objc_getClass("__NSDictionaryM");
Method originMethod = class_getInstanceMethod(o_class, orginSel);
Method overrideMethod = class_getInstanceMethod(o_class, overrideSel);
//原来的类没有实现指定的方法,那么我们就得先做判断,把方法添加进去,然后进行替换
if (class_addMethod(o_class, orginSel, method_getImplementation(overrideMethod) , method_getTypeEncoding(originMethod))) {
class_replaceMethod(o_class,
overrideSel,
method_getImplementation(originMethod),
method_getTypeEncoding(originMethod));
}else{
//交换实现
method_exchangeImplementations(originMethod, overrideMethod);
}
});
}
- (void)overrideSetObject:(id)anObject forKey:(id <NSCopying>)aKey;
{
if (anObject) {
NSLog(@"%@--overrideSetObject",self);
/** 注意:必须调用自己的方法名 */
[self overrideSetObject:anObject forKey:aKey];
}
}
@end
/**
类 “真身”
NSArray __NSArrayI
NSMutableArray __NSArrayM
NSDictionary __NSDictionaryI
NSMutableDictionary __NSDictionaryM
*/
2.以上另外一个用法,兼容字典传空;同理NSArray也可以这么写一个,需要注意的是类的真身是什么,要针对性的写