先来看看巴博萨是如何封盖詹姆斯的....腋毛
OK!进入正题,哈哈😀
什么是Method Swizzling?
字面上意思:方法调和,也就是方法交换,其中交换的是方法的实现。具体点的来说,我们用
@selector
(方法选择器) 取出来的是一个方法的编号(指向方法的指针) ,用SEL
类型表示,它所指向的是一个IMP
(方法实现的指针) ,而我们交换的就是这个IMP
,从而达到方法实现交换的效果。
有什么作用?
我只能说一下自己的理解,当一个方法在工程中大量被调用时,我们想要批量替换或修改,那就很麻烦,有人说直接修改这个方法的实现,这种方式是不推荐的,因为这会破坏原有方法的完整性,而且也不是所有方法都能被修改,比如闭源,这个时候我们用Method Swizzling就可以很好的处理了。
怎么使用?
首先导入<objc/runtime.h>
,其中有一个这样的API:method_exchangeImplementations(Method m1, Method m2)
,意思也很明显就是方法实现交换,我这里贴上两个封装好的方法:
/**
交换两个对象方法的实现
@param srcClass 被替换方法的类
@param srcSel 被替换的方法编号
@param swizzledSel 用于替换的方法编号
*/
- (void)zm_swizzleInstanceMethodWithSrcClass:(Class)srcClass
srcSel:(SEL)srcSel
swizzledSel:(SEL)swizzledSel{
Method srcMethod = class_getInstanceMethod(srcClass, srcSel);
Method swizzledMethod = class_getInstanceMethod([self class], swizzledSel);
if (!srcClass || !srcMethod || !swizzledMethod) return;
//加一层保护措施,如果添加成功,则表示该方法不存在于本类,而是存在于父类中,不能交换父类的方法,否则父类的对象调用该方法会crash;添加失败则表示本类存在该方法
BOOL addMethod = class_addMethod(srcClass, srcSel, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
if (addMethod)
{
//再将原有的实现替换到swizzledMethod方法上,从而实现方法的交换,并且未影响到父类方法的实现
class_replaceMethod(srcClass, swizzledSel, method_getImplementation(srcMethod), method_getTypeEncoding(srcMethod));
}else
{
method_exchangeImplementations(srcMethod, swizzledMethod);
}
}
/**
交换两个类方法的实现
@param srcClass 被替换方法的类
@param srcSel 被替换的方法编号
@param swizzledSel 用于替换的方法编号
*/
- (void)zm_swizzleClassMethodWithSrcClass:(Class)srcClass
srcSel:(SEL)srcSel
swizzledSel:(SEL)swizzledSel{
Method srcMethod = class_getClassMethod(srcClass, srcSel);
Method swizzledMethod = class_getClassMethod([self class], swizzledSel);
if (!srcClass || !srcMethod || !swizzledMethod) return;
//注意:类方法是存在于元类中,所以要添加到元类
srcClass = objc_getMetaClass(class_getName(srcClass));
BOOL addMethod = class_addMethod(srcClass, srcSel, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
if (addMethod)
{
class_replaceMethod(srcClass, swizzledSel, method_getImplementation(srcMethod), method_getTypeEncoding(srcMethod));
}else
{
method_exchangeImplementations(srcMethod, swizzledMethod);
}
}
举几个栗子
-
友盟统计很多人用过吧,每个控制器页面出现和消失都要做标记,很烦!所以我们用Method Swizzling对
viewWillAppear
&viewWillDisappear
跟我们自定义方法交换,然后统一处理@implementation UIViewController (Swizzle) +(void)load{ static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ [self zm_swizzleInstanceMethodWithSrcClass:[self class] srcSel:@selector(viewWillAppear:) swizzledSel:@selector(zm_ViewWillAppear:)]; [self zm_swizzleInstanceMethodWithSrcClass:[self class] srcSel:@selector(viewWillDisappear:) swizzledSel:@selector(zm_ViewWillDisappear:)]; }); } /** 页面出现的时候会进入到这里实现,即使在子类重写了ViewWillAppear:方法, 那么在调用[super ViewWillAppear:animated]的时候还是会进入这里。 @param animated 动画 */ - (void)zm_ViewWillAppear:(BOOL)animated{ //此处调用自己其实就是调用UIViewController的viewWillAppear的原生实现方法。 [self zm_ViewWillAppear:animated]; //统一添加统计代码 self.title.length == 0?:[MobClick beginLogPageView:self.title]; } - (void)zm_ViewWillDisappear:(BOOL)animated{ [self zm_ViewWillDisappear:animated]; self.title.length == 0?:[MobClick endLogPageView:self.title]; }
-
对一些系统原生方法做调换,防止开发过程中因不谨慎导致的crash,比如数组越界、数组和字典插入nil对象、字符串截取越界...,我们都可以在自定义方法中先做判断,从而过滤掉这些不注意的bug,一劳永逸。这里只对一些常用的方法做处理
不可变数组:
@implementation NSArray (ZMSafe) //数组初始化类 static NSString *KInitArrayClass = @"__NSPlaceholderArray"; //空元素数组类,空数组 static NSString *KEmptyArrayClass = @"__NSArray0"; //单元素数组类,一个元素的数组 static NSString *KSingleArrayClass = @"__NSSingleObjectArrayI"; //多元素数组类,两个元素以上的数组 static NSString *KMultiArrayClass = @"__NSArrayI"; #define KSelectorFromString(s1,s2) NSSelectorFromString([NSString stringWithFormat:@"%@%@",s1,s2]) +(void)load { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ [self zm_swizzleInstanceMethodWithSrcClass:NSClassFromString(KInitArrayClass) srcSel:@selector(initWithObjects:count:) swizzledSel:@selector(zm_safeInitWithObjects:count:)]; [self zm_arrayMethodSwizzleWithRealClass:KEmptyArrayClass prefix:@"zm_emptyArray"]; [self zm_arrayMethodSwizzleWithRealClass:KSingleArrayClass prefix:@"zm_singleArray"]; [self zm_arrayMethodSwizzleWithRealClass:KMultiArrayClass prefix:@"zm_multiArray"]; }); } + (void)zm_arrayMethodSwizzleWithRealClass:(NSString *)realClass prefix:(NSString *)prefix { [self zm_swizzleInstanceMethodWithSrcClass:NSClassFromString(realClass) srcSel:@selector(objectAtIndex:) swizzledSel:KSelectorFromString(prefix, @"ObjectAtIndex:")]; [self zm_swizzleInstanceMethodWithSrcClass:NSClassFromString(realClass) srcSel:@selector(arrayByAddingObject:) swizzledSel:KSelectorFromString(prefix, @"ArrayByAddingObject:")]; if (iOS11) { [self zm_swizzleInstanceMethodWithSrcClass:NSClassFromString(realClass) srcSel:@selector(objectAtIndexedSubscript:) swizzledSel:KSelectorFromString(prefix, @"ObjectAtIndexedSubscript:")]; } } #pragma mark -- swizzled Methods - (instancetype)zm_safeInitWithObjects:(id *)objects count:(NSUInteger)cnt { for (NSUInteger i = 0; i < cnt; i++) { if (!objects[i]) objects[i] = @""; } return [self zm_safeInitWithObjects:objects count:cnt]; } - (id)zm_emptyArrayObjectAtIndex:(NSUInteger)index { if (index >= self.count) return nil; return [self zm_emptyArrayObjectAtIndex:index]; } - (id)zm_singleArrayObjectAtIndex:(NSUInteger)index { if (index >= self.count) return nil; return [self zm_singleArrayObjectAtIndex:index]; } - (id)zm_multiArrayObjectAtIndex:(NSUInteger)index { if (index >= self.count) return nil; // NSLog(@"%@",NSStringFromClass(self.class)); //__NSArrayI // NSLog(@"%@",NSStringFromClass(self.superclass)); //NSArray // __NSArrayI是NSArray的子类 // zm_multiArrayObjectAtIndex:是NSArray的方法,self是__NSArrayI的实例,子类调用父类的方法,没问题 return [self zm_multiArrayObjectAtIndex:index]; } //解决array[index] 字面量语法超出界限的bug - (id)zm_emptyArrayObjectAtIndexedSubscript:(NSUInteger)index { if (index >= self.count) return nil; return [self zm_emptyArrayObjectAtIndexedSubscript:index]; } - (id)zm_singleArrayObjectAtIndexedSubscript:(NSUInteger)index { if (index >= self.count) return nil; return [self zm_singleArrayObjectAtIndexedSubscript:index]; } - (id)zm_multiArrayObjectAtIndexedSubscript:(NSUInteger)index { if (index >= self.count) return nil; return [self zm_multiArrayObjectAtIndexedSubscript:index]; } - (NSArray*)zm_emptyArrayArrayByAddingObject:(id)anObject { if(!anObject) return self; return [self zm_emptyArrayArrayByAddingObject:anObject]; } - (NSArray*)zm_singleArrayArrayByAddingObject:(id)anObject { if(!anObject) return self; return [self zm_singleArrayArrayByAddingObject:anObject]; } - (NSArray*)zm_multiArrayArrayByAddingObject:(id)anObject { if(!anObject) return self; return [self zm_multiArrayArrayByAddingObject:anObject]; }
可变数组:
这里使用MRC写法,是为了修复[UIKeyboardLayoutStar release]: message sent to deallocated instance的Bug,所以该文件需要添加ARC支持-fno-objc-arc
@implementation NSMutableArray (ZMSafe)
static NSString *KMArrayClass = @"__NSArrayM";+(void)load{ static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ @autoreleasepool { [self zm_swizzleInstanceMethodWithSrcClass:NSClassFromString(KMArrayClass) srcSel:@selector(addObject:) swizzledSel:@selector(zm_safeAddObject:)]; [self zm_swizzleInstanceMethodWithSrcClass:NSClassFromString(KMArrayClass) srcSel:@selector(insertObject:atIndex:) swizzledSel:@selector(zm_safeInsertObject:atIndex:)]; [self zm_swizzleInstanceMethodWithSrcClass:NSClassFromString(KMArrayClass) srcSel:@selector(removeObjectAtIndex:) swizzledSel:@selector(zm_safeRemoveObjectAtIndex:)]; [self zm_swizzleInstanceMethodWithSrcClass:NSClassFromString(KMArrayClass) srcSel:@selector(replaceObjectAtIndex:withObject:) swizzledSel:@selector(zm_safeReplaceObjectAtIndex:withObject:)]; [self zm_swizzleInstanceMethodWithSrcClass:NSClassFromString(KMArrayClass) srcSel:@selector(objectAtIndex:) swizzledSel:@selector(zm_safeObjectAtIndex:)]; if (iOS11) { [self zm_swizzleInstanceMethodWithSrcClass:NSClassFromString(KMArrayClass) srcSel:@selector(objectAtIndexedSubscript:) swizzledSel:@selector(zm_safeObjectAtIndexedSubscript:)]; } }); } - (void)zm_safeAddObject:(id)anObject{ @autoreleasepool { if(!anObject)return; [self zm_safeAddObject:anObject]; } } - (void)zm_safeInsertObject:(id)anObject atIndex:(NSUInteger)index{ @autoreleasepool { if(!anObject || index > self.count)return; [self zm_safeInsertObject:anObject atIndex:index]; } } - (void)zm_safeRemoveObjectAtIndex:(NSUInteger)index{ @autoreleasepool { if(index >= self.count) return; [self zm_safeRemoveObjectAtIndex:index]; } } - (void)zm_safeReplaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject{ @autoreleasepool { if(index >= self.count || !anObject) return; [self zm_safeReplaceObjectAtIndex:index withObject:anObject]; } } - (id)zm_safeObjectAtIndex:(NSUInteger)index{ @autoreleasepool { if (index >= self.count) return nil; return [self zm_safeObjectAtIndex:index]; } } - (id)zm_safeObjectAtIndexedSubscript:(NSUInteger)index{ @autoreleasepool { if (index >= self.count) return nil; return [self zm_safeObjectAtIndexedSubscript:index]; } }
不可变字典:
@implementation NSDictionary (ZMSafe) static NSString *KDictionaryClass = @"__NSPlaceholderDictionary"; +(void)load{ static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ [self zm_swizzleInstanceMethodWithSrcClass:NSClassFromString(KDictionaryClass) srcSel:@selector(initWithObjects:forKeys:count:) swizzledSel:@selector(zm_safeInitWithObjects:forKeys:count:)]; }); } - (instancetype)zm_safeInitWithObjects:(id*)objects forKeys:(id*)keys count: (NSUInteger)cnt{ for (NSUInteger i = 0; i < cnt; i++) { if(!keys[i]) keys[i] = @""; if(!objects[i]) objects[i] = @""; } return [self zm_safeInitWithObjects:objects forKeys:keys count:cnt]; }
可变字典:
@implementation NSMutableDictionary (ZMSafe) static NSString *KMDictionaryClass = @"__NSDictionaryM"; +(void)load{ static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ [self zm_swizzleInstanceMethodWithSrcClass:NSClassFromString(KMDictionaryClass) srcSel:@selector(setObject:forKey:) swizzledSel:@selector(zm_safeSetObject:forKey:)]; }); } - (void)zm_safeSetObject:(id)anObject forKey:(id <NSCopying>)aKey{ if(!anObject || !aKey) return; [self zm_safeSetObject:anObject forKey:aKey]; }
不可变字符串:
@implementation NSString (ZMSafe) static NSString *KStringClass = @"__NSCFString"; +(void)load{ static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ [self zm_swizzleInstanceMethodWithSrcClass:NSClassFromString(KStringClass) srcSel:@selector(characterAtIndex:) swizzledSel:@selector(zm_safeCharacterAtIndex:)]; [self zm_swizzleInstanceMethodWithSrcClass:NSClassFromString(KStringClass) srcSel:@selector(substringWithRange:) swizzledSel:@selector(zm_safeSubstringWithRange:)]; [self zm_swizzleInstanceMethodWithSrcClass:NSClassFromString(KStringClass) srcSel:@selector(substringFromIndex:) swizzledSel:@selector(zm_safeSubstringFromIndex:)]; [self zm_swizzleInstanceMethodWithSrcClass:NSClassFromString(KStringClass) srcSel:@selector(substringToIndex:) swizzledSel:@selector(zm_safeSubstringToIndex:)]; [self zm_swizzleInstanceMethodWithSrcClass:NSClassFromString(KStringClass) srcSel:@selector(rangeOfString:) swizzledSel:@selector(zm_safeRangeOfString:)]; }); } - (unichar)zm_safeCharacterAtIndex:(NSUInteger)index{ if(index >= self.length) return 0; return [self zm_safeCharacterAtIndex:index]; } - (NSString *)zm_safeSubstringFromIndex:(NSUInteger)from{ if(from > self.length) return @""; return [self zm_safeSubstringFromIndex:from]; } - (NSString *)zm_safeSubstringToIndex:(NSUInteger)to{ if(to > self.length) return self; return [self zm_safeSubstringToIndex:to]; } - (NSString *)zm_safeSubstringWithRange:(NSRange)range{ if(range.location + range.length > self.length) return @""; return [self zm_safeSubstringWithRange:range]; } - (NSRange)zm_safeRangeOfString:(NSString *)searchString{ if(!searchString) return NSMakeRange(0, 0); return [self zm_safeRangeOfString:searchString]; }
可变字符串:
@implementation NSMutableString (ZMSafe) static NSString *KMStringClass = @"__NSCFConstantString"; +(void)load{ static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ [self zm_swizzleInstanceMethodWithSrcClass:NSClassFromString(KMStringClass) srcSel:@selector(replaceCharactersInRange:withString:) swizzledSel:@selector(zm_safeReplaceCharactersInRange:withString:)]; [self zm_swizzleInstanceMethodWithSrcClass:NSClassFromString(KMStringClass) srcSel:@selector(insertString:atIndex:) swizzledSel:@selector(zm_safeInsertString:atIndex:)]; [self zm_swizzleInstanceMethodWithSrcClass:NSClassFromString(KMStringClass) srcSel:@selector(deleteCharactersInRange:) swizzledSel:@selector(zm_safeDeleteCharactersInRange:)]; [self zm_swizzleInstanceMethodWithSrcClass:NSClassFromString(KMStringClass) srcSel:@selector(appendString:) swizzledSel:@selector(zm_safeAppendString:)]; [self zm_swizzleInstanceMethodWithSrcClass:NSClassFromString(KMStringClass) srcSel:@selector(setString:) swizzledSel:@selector(zm_safeSetString:)]; }); } - (void)zm_safeReplaceCharactersInRange:(NSRange)range withString:(NSString *)aString{ if(range.location + range.length > self.length || !aString) return; [self zm_safeReplaceCharactersInRange:range withString:aString]; } - (void)zm_safeInsertString:(NSString *)aString atIndex:(NSUInteger)loc{ if (!aString || loc > self.length) return; [self zm_safeInsertString:aString atIndex:loc]; } - (void)zm_safeDeleteCharactersInRange:(NSRange)range{ if(range.location + range.length > self.length) return; [self zm_safeDeleteCharactersInRange:range]; } - (void)zm_safeAppendString:(NSString *)aString{ if(!aString) return; [self zm_safeAppendString:aString]; } - (void)zm_safeSetString:(NSString *)aString{ if(!aString) return; [self zm_safeSetString:aString]; }
就列这些吧,后续想到了在加,更多的是提供一种思路,开发中可以灵活运用,但是也不能乱用,不然会出现意想不到的后果😀
注意几点
- 最好在
+(load)
方法中来调用,因为+(load)
能确保一定被调用,而且调用时机非常早,在装载类文件时就会被调用(程序启动前),所以在运行时你的Method Swizzling一定是执行了的。 - 在执行Method Swizzling的时候最好加上
dispatch_once
,虽然说+(load)
只会被系统调用一次,但是如果在子类或子类的子类调用了[super load]
,那么父类会再次调用,导致Method Swizzling多次执行,换来换去,到时候有没有调换过来就看运气了,那就尴尬了😀 - 如果交换的是系统原生方法,一定要在自定义实现方法中调用自身方法,也就是原生实现方法,因为我们不是要整个重写原生实现方法,而是在它基础之上添加我们自己的东西;如果交换的是自定义方法,那就看需求而定咯😀