咱們先來了解下 Objective-C 中方法 Method 的數據結構:html
typedef struct method_t *Method; struct method_t { SEL name; const char *types; IMP imp; struct SortBySELAddress : public std::binary_function{ bool operator() (const method_t& lhs, const method_t& rhs) { return lhs.name < rhs.name; } }; }; 複製代碼
本質上,它就是 struct method_t 類型的指針,因此咱們重點看下結構體 method_t 的定義。在結構體 method_t 中定義了三個成員變量和一個成員函數:ios
- name 表示的是方法的名稱,用於惟一標識某個方法,好比 @selector(viewWillAppear:) ;
- types 表示的是方法的返回值和參數類型(詳細信息能夠查閱蘋果官方文檔中的 Type Encodings);
- imp 是一個函數指針,指向方法的實現;
- SortBySELAddress 顧名思義,是一個根據 name 的地址對方法進行排序的函數。
由此,咱們也能夠發現 Objective-C 中的方法名是不包括參數類型的,也就是說下面兩個方法在 runtime 看來就是同一個方法:編程
- (void)viewWillAppear:(BOOL)animated;
- (void)viewWillAppear:(NSString *)string;複製代碼
而下面兩個方法倒是能夠共存的:數組
- (void)viewWillAppear:(BOOL)animated;
+ (void)viewWillAppear:(BOOL)animated;複製代碼
由於實例方法和類方法是分別保存在類對象和元類對象中的.安全
原則上,方法的名稱 name 和方法的實現 imp 是一一對應的,而 Method Swizzling 的原理就是動態地改變它們的對應關係,以達到替換方法實現的目的。數據結構
那麼何時咱們才須要使用 Method Swizzling呢?多線程
咱們主要是用它來把系統的方法交換爲咱們本身的方法,從而給系統方法添加一些咱們想要的功能。函數
下面會列舉諸多使用場景,讓你們見識一下 Method Swizzling 的強大之處。字體
因爲method swizzling的實現模式是固定的,因此咱們能夠抽成一個方法,做爲NSobject的分類,之後能夠直接調用便可。ui
@interface NSObject (Swizzling) + (void)methodSwizzlingWithOriginalSelector:(SEL)originalSelector bySwizzledSelector:(SEL)swizzledSelector; @end ==================================================================== #import "NSObject+MethodSwizzling.h" #import@implementation NSObject (MethodSwizzling) + (void)methodSwizzlingWithOriginalSelector:(SEL)originalSelector bySwizzledSelector:(SEL)swizzledSelector { Class class = [self class]; Method originalMethod = class_getInstanceMethod(class, originalSelector); Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector); //先嚐試給源SEL添加IMP,這裏是爲了不源SEL沒有實現IMP的狀況 BOOL didAddMethod = class_addMethod(class,originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod)); if (didAddMethod) {//添加成功:說明源SEL沒有實現IMP,將源SEL的IMP替換到交換SEL的IMP class_replaceMethod(class,swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)); } else {//添加失敗:說明源SEL已經有IMP,直接將兩個SEL的IMP交換便可 method_exchangeImplementations(originalMethod, swizzledMethod); } } @end 複製代碼
#import "UIViewController+MethodSwizzling.h"
#import "NSObject+MethodSwizzling.h"
@implementation UIViewController (MethodSwizzling)
+(void)load{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[self methodSwizzlingWithOriginalSelector:@selector(viewWillAppear:) bySwizzledSelector:@selector(my_viewWillAppear:) ];
});
}
-(void)my_viewWillAppear:(BOOL)animated{
NSLog(@"調用了本身定義的viewWillAppear方法");
[self my_viewWillAppear:YES];
}
@end複製代碼
爲何要在+(void)load方法裏面調用method swizzling?
答案:
+load 和 +initialize 是 Objective-C runtime 會自動調用的兩個類方法。可是它們被調用的時機倒是有差異的,+load 方法是在類被加載的時候調用的,也就是必定會被調用。而 +initialize 方法是在類或它的子類收到第一條消息以前被調用的,這裏所指的消息包括實例方法和類方法的調用。也就是說 +initialize 方法是以懶加載的方式被調用的,若是程序一直沒有給某個類或它的子類發送消息,那麼這個類的 +initialize 方法是永遠不會被調用的。此外 +load 方法還有一個很是重要的特性,那就是子類、父類和分類中的 +load 方法的實現是被區別對待的。換句話說在 Objective-C runtime 自動調用 +load 方法時,分類中的 +load 方法並不會對主類中的 +load 方法形成覆蓋。綜上所述,+load 方法是實現 Method Swizzling 邏輯的最佳「場所」。
爲何使用dispatch_once執行方法交換?
答案:
方法交換應該要線程安全,並且保證在任何狀況下(多線程環境,或者被其餘人手動再次調用+load方法)只交換一次,除非只是臨時交換使用,在使用完成後又交換回來。 最經常使用的用法是在+load方法中使用dispatch_once來保證交換是安全的。
當咱們對一個NSMutableArray插入或者添加一個nil會致使崩潰,或者須要獲取、移除的對象超過了數組的最大邊界,就會出現數組越界,也會致使崩潰。
這個時候咱們可使用method swizzling來解決該問題,讓數組在上述狀況不會崩潰。
#import "NSObject+MethodSwizzling.h" #import "NSMutableArray+SWmethod.h" #import@implementation NSMutableArray (SWmethod) + (void)load { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ [objc_getClass("__NSArrayM") methodSwizzlingWithOriginalSelector:@selector(removeObject:) bySwizzledSelector:@selector(safeRemoveObject:) ]; [objc_getClass("__NSArrayM") methodSwizzlingWithOriginalSelector:@selector(addObject:) bySwizzledSelector:@selector(safeAddObject:)]; [objc_getClass("__NSArrayM") methodSwizzlingWithOriginalSelector:@selector(removeObjectAtIndex:) bySwizzledSelector:@selector(safeRemoveObjectAtIndex:)]; [objc_getClass("__NSArrayM") methodSwizzlingWithOriginalSelector:@selector(insertObject:atIndex:) bySwizzledSelector:@selector(safeInsertObject:atIndex:)]; [objc_getClass("__NSArrayM") methodSwizzlingWithOriginalSelector:@selector(objectAtIndex:) bySwizzledSelector:@selector(safeObjectAtIndex:)]; }); } - (void)safeAddObject:(id)obj { if (obj == nil) { NSLog(@"%s can add nil object into NSMutableArray", __FUNCTION__); } else { [self safeAddObject:obj]; } } - (void)safeRemoveObject:(id)obj { if (obj == nil) { NSLog(@"%s call -removeObject:, but argument obj is nil", __FUNCTION__); return; } [self safeRemoveObject:obj]; } - (void)safeInsertObject:(id)anObject atIndex:(NSUInteger)index { if (anObject == nil) { NSLog(@"%s can't insert nil into NSMutableArray", __FUNCTION__); } else if (index > self.count) { NSLog(@"%s index is invalid", __FUNCTION__); } else { [self safeInsertObject:anObject atIndex:index]; } } - (id)safeObjectAtIndex:(NSUInteger)index { if (self.count == 0) { NSLog(@"%s can't get any object from an empty array", __FUNCTION__); return nil; } if (index > self.count) { NSLog(@"%s index out of bounds in array", __FUNCTION__); return nil; } return [self safeObjectAtIndex:index]; } - (void)safeRemoveObjectAtIndex:(NSUInteger)index { if (self.count <= 0)="" {="" nslog(@"%s="" can't="" get="" any="" object="" from="" an="" empty="" array",="" __function__);="" return;="" }="" if="" (index="">= self.count) { NSLog(@"%s index out of bound", __FUNCTION__); return; } [self safeRemoveObjectAtIndex:index]; } @end 複製代碼
看以下代碼
[objc_getClass("__NSArrayM") methodSwizzlingWithOriginalSelector:@selector(removeObject:) bySwizzledSelector:@selector(safeRemoveObject:) ];複製代碼
這裏沒有使用self來調用,而是使用objc_getClass("__NSArrayM")來調用的。由於NSMutableArray的真實類只能經過後者來獲取,而不能經過[self class]來獲取,而method swizzling只對真實的類起做用。
這裏就涉及到一個小知識點:類簇。
在iOS中存在大量的類簇,好比咱們經常使用的NSArray,NSDictonary等等
你們發現了嗎,__NSArrayI纔是NSArray真正的類,而NSMutableArray又不同。咱們能夠經過runtime函數獲取真正的類:
objc_getClass("__NSArrayI")複製代碼
有關類簇的更多知識,你們能夠看一下兩篇博客:
上面只是展現瞭如何避免NSMutableArray的崩潰,主要是在原有的系統方法裏面加上了判斷。據此你們能夠本身實現NSArray、NSDictonary的崩潰處理
在項目比較成熟的基礎上,遇到了這樣一個需求,應用中須要引入新的字體,須要更換全部Label的默認字體,可是同時,對於一些特殊設置了字體的label又不須要更換。乍看起來,這個問題確實十分棘手,首先項目比較大,一個一個設置全部使用到的label的font工做量是巨大的,而且在許多動態展現的界面中,可能會漏掉一些label,產生bug。其次,項目中的label來源並不惟一,有用代碼建立的,有xib和storyBoard中的,這也將浪費很大的精力。
這是最簡單方便的方法,咱們可使用runtime機制替換掉UILabel的初始化方法,在其中對label的字體進行默認設置。由於Label能夠從initWithFrame、init和nib文件三個來源初始化,因此咱們須要將這三個初始化的方法都替換掉。
#import "UILabel+YHBaseChangeDefaultFont.h" #import@implementation UILabel (YHBaseChangeDefaultFont) /** *每一個NSObject的子類都會調用下面這個方法 在這裏將init方法進行替換,使用咱們的新字體 *若是在程序中又特殊設置了字體 則特殊設置的字體不會受影響 可是不要在Label的init方法中設置字體 *從init和initWithFrame和nib文件的加載方法 都支持更換默認字體 */ +(void)load{ //只執行一次這個方法 static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ Class class = [self class]; // When swizzling a class method, use the following: // Class class = object_getClass((id)self); //替換三個方法 SEL originalSelector = @selector(init); SEL originalSelector2 = @selector(initWithFrame:); SEL originalSelector3 = @selector(awakeFromNib); SEL swizzledSelector = @selector(YHBaseInit); SEL swizzledSelector2 = @selector(YHBaseInitWithFrame:); SEL swizzledSelector3 = @selector(YHBaseAwakeFromNib); Method originalMethod = class_getInstanceMethod(class, originalSelector); Method originalMethod2 = class_getInstanceMethod(class, originalSelector2); Method originalMethod3 = class_getInstanceMethod(class, originalSelector3); Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector); Method swizzledMethod2 = class_getInstanceMethod(class, swizzledSelector2); Method swizzledMethod3 = class_getInstanceMethod(class, swizzledSelector3); BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod)); BOOL didAddMethod2 = class_addMethod(class, originalSelector2, method_getImplementation(swizzledMethod2), method_getTypeEncoding(swizzledMethod2)); BOOL didAddMethod3 = class_addMethod(class, originalSelector3, method_getImplementation(swizzledMethod3), method_getTypeEncoding(swizzledMethod3)); if (didAddMethod) { class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)); } else { method_exchangeImplementations(originalMethod, swizzledMethod); } if (didAddMethod2) { class_replaceMethod(class, swizzledSelector2, method_getImplementation(originalMethod2), method_getTypeEncoding(originalMethod2)); }else { method_exchangeImplementations(originalMethod2, swizzledMethod2); } if (didAddMethod3) { class_replaceMethod(class, swizzledSelector3, method_getImplementation(originalMethod3), method_getTypeEncoding(originalMethod3)); }else { method_exchangeImplementations(originalMethod3, swizzledMethod3); } }); } /** *在這些方法中將你的字體名字換進去 */ - (instancetype)YHBaseInit { id __self = [self YHBaseInit]; UIFont * font = [UIFont fontWithName:@"這裏輸入你的字體名字" size:self.font.pointSize]; if (font) { self.font=font; } return __self; } -(instancetype)YHBaseInitWithFrame:(CGRect)rect{ id __self = [self YHBaseInitWithFrame:rect]; UIFont * font = [UIFont fontWithName:@"這裏輸入你的字體名字" size:self.font.pointSize]; if (font) { self.font=font; } return __self; } -(void)YHBaseAwakeFromNib{ [self YHBaseAwakeFromNib]; UIFont * font = [UIFont fontWithName:@"這裏輸入你的字體名字" size:self.font.pointSize]; if (font) { self.font=font; } } @end 複製代碼
想象這樣一個場景,出於某些需求,咱們須要跟蹤記錄APP中按鈕的點擊次數和頻率等數據,怎麼解決?固然經過繼承按鈕類或者經過類別實現是一個辦法,可是帶來其餘問題好比別人不必定會去實例化你寫的子類,或者其餘類別也實現了點擊方法致使不肯定會調用哪個,抖機靈的方法以下:
@implementation UIButton (Hook)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Class selfClass = [self class];
SEL oriSEL = @selector(sendAction:to:forEvent:);
Method oriMethod = class_getInstanceMethod(selfClass, oriSEL);
SEL cusSEL = @selector(mySendAction:to:forEvent:);
Method cusMethod = class_getInstanceMethod(selfClass, cusSEL);
BOOL addSucc = class_addMethod(selfClass, oriSEL, method_getImplementation(cusMethod), method_getTypeEncoding(cusMethod));
if (addSucc) {
class_replaceMethod(selfClass, cusSEL, method_getImplementation(oriMethod), method_getTypeEncoding(oriMethod));
}else {
method_exchangeImplementations(oriMethod, cusMethod);
}
});
}
- (void)mySendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
[CountTool addClickCount];
[self mySendAction:action to:target forEvent:event];
}
@end複製代碼
method swizzling方法的強大之處在於能夠替換的系統的方法實現,添加本身想要的功能。上面的一些使用場景都是網上找的一些經典例子,你們能夠觸類旁通。
更多文章歡迎關注個人我的blog