轉(zhuǎn)自:http://tech.glowing.com/cn/method-swizzling-aop/
上一篇介紹了 Objective-C Messaging饼丘。利用 Objective-C 的 Runtime 特性,我們可以給語言做擴展辽话,幫助解決項目開發(fā)中的一些設(shè)計和技術(shù)問題肄鸽。這一篇,我們來探索一些利用 Objective-C Runtime 的黑色技巧油啤。這些技巧中最具爭議的或許就是 Method Swizzling 典徘。
介紹一個技巧,最好的方式就是提出具體的需求益咬,然后用它跟其他的解決方法做比較逮诲。
所以,先來看看我們的需求:對 App 的用戶行為進行追蹤和分析幽告。簡單說梅鹦,就是當(dāng)用戶看到某個 View
或者點擊某個 Button
的時候,就把這個事件記下來冗锁。
手動添加
最直接粗暴的方式就是在每個 viewDidAppear
里添加記錄事件的代碼齐唆。
@implementation MyViewController ()- (void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; // Custom code // Logging [Logging logWithEventName:@“my view did appear”];}- (void)myButtonClicked:(id)sender{ // Custom code // Logging [Logging logWithEventName:@“my button clicked”];}
這種方式的缺點也很明顯:它破壞了代碼的干凈整潔。因為 Logging
的代碼本身并不屬于 ViewController
里的主要邏輯冻河。隨著項目擴大箍邮、代碼量增加,你的 ViewController
里會到處散布著 Logging
的代碼叨叙。這時锭弊,要找到一段事件記錄的代碼會變得困難,也很容易忘記添加事件記錄的代碼擂错。
你可能會想到用繼承或類別味滞,在重寫的方法里添加事件記錄的代碼。代碼可以是長的這個樣子:
@implementation UIViewController ()- (void)myViewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; // Custom code // Logging [Logging logWithEventName:NSStringFromClass([self class])];}- (void)myButtonClicked:(id)sender{ // Custom code // Logging NSString *name = [NSString stringWithFormat:@“my button in %@ is clicked”, NSStringFromClass([self class])]; [Logging logWithEventName:name];}
Logging
的代碼都很相似,通過繼承或類別重寫相關(guān)方法是可以把它從主要邏輯中剝離出來剑鞍。但同時也帶來新的問題:
你需要繼承 UIViewController
, UITableViewController
, UICollectionViewController
所有這些 ViewController 刹悴,或者給他們添加類別;
每個 ViewController 里的 ButtonClick 方法命名不可能都一樣攒暇;
你不能控制別人如何去實例化你的子類;
對于類別子房,你沒辦法調(diào)用到原來的方法實現(xiàn)形用。大多時候,我們重寫一個方法只是為了添加一些代碼证杭,而不是完全取代它田度。
如果有兩個類別都實現(xiàn)了相同的方法,運行時沒法保證哪一個類別的方法會給調(diào)用解愤。
Method Swizzling
Method Swizzling 利用 Runtime 特性把一個方法的實現(xiàn)與另一個方法的實現(xiàn)進行替換镇饺。
上一篇文章 有講到每個類里都有一個 Dispatch Table ,將方法的名字(SEL)跟方法的實現(xiàn)(IMP送讲,指向 C 函數(shù)的指針)一一對應(yīng)奸笤。Swizzle 一個方法其實就是在程序運行時在 Dispatch Table 里做點改動,讓這個方法的名字(SEL)對應(yīng)到另個 IMP 哼鬓。
首先定義一個類別监右,添加將要 Swizzled 的方法:
@implementation UIViewController (Logging)- (void)swizzled_viewDidAppear:(BOOL)animated{ // call original implementation [self swizzled_viewDidAppear:animated]; // Logging [Logging logWithEventName:NSStringFromClass([self class])];}
代碼看起來可能有點奇怪,像遞歸不是么异希。當(dāng)然不會是遞歸健盒,因為在 runtime 的時候,函數(shù)實現(xiàn)已經(jīng)被交換了称簿。調(diào)用 viewDidAppear:
會調(diào)用你實現(xiàn)的 swizzled_viewDidAppear:
扣癣,而在 swizzled_viewDidAppear:
里調(diào)用 swizzled_viewDidAppear:
實際上調(diào)用的是原來的 viewDidAppear:
。
接下來實現(xiàn) swizzle 的方法 :
@implementation UIViewController (Logging)void swizzleMethod(Class class, SEL originalSelector, SEL swizzledSelector) { // the method might not exist in the class, but in its superclass Method originalMethod = class_getInstanceMethod(class, originalSelector); Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector); // class_addMethod will fail if original method already exists BOOL didAddMethod = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod)); // the method doesn’t exist and we just added one if (didAddMethod) { class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod)); } else { method_exchangeImplementations(originalMethod, swizzledMethod); }}
這里唯一可能需要解釋的是 class_addMethod
憨降。要先嘗試添加原 selector 是為了做一層保護父虑,因為如果這個類沒有實現(xiàn) originalSelector
,但其父類實現(xiàn)了券册,那 class_getInstanceMethod
會返回父類的方法频轿。這樣 method_exchangeImplementations
替換的是父類的那個方法,這當(dāng)然不是你想要的烁焙。所以我們先嘗試添加 orginalSelector
航邢,如果已經(jīng)存在,再用 method_exchangeImplementations
把原方法的實現(xiàn)跟新的方法實現(xiàn)給交換掉骄蝇。
最后膳殷,我們只需要確保在程序啟動的時候調(diào)用 swizzleMethod
方法。比如,我們可以在之前 UIViewController
的 Logging 類別里添加 +load:
方法赚窃,然后在 +load:
里把 viewDidAppear
給替換掉:
@implementation UIViewController (Logging)+ (void)load{ swizzleMethod([self class], @selector(viewDidAppear:), @selector(swizzled_viewDidAppear:));}
一般情況下册招,類別里的方法會重寫掉主類里相同命名的方法。如果有兩個類別實現(xiàn)了相同命名的方法勒极,只有一個方法會被調(diào)用是掰。但 +load:
是個特例,當(dāng)一個類被讀到內(nèi)存的時候辱匿, runtime 會給這個類及它的每一個類別都發(fā)送一個 +load:
消息键痛。
其實,這里還可以更簡化點:直接用新的 IMP 取代原 IMP 匾七,而不是替換絮短。只需要有全局的函數(shù)指針指向原 IMP 就可以。
void (gOriginalViewDidAppear)(id, SEL, BOOL);void newViewDidAppear(UIViewController *self, SEL _cmd, BOOL animated) { // call original implementation gOriginalViewDidAppear(self, _cmd, animated); // Logging [Logging logWithEventName:NSStringFromClass([self class])];}+ (void)load{ Method originalMethod = class_getInstanceMethod(self, @selector(viewDidAppear:)); gOriginalViewDidAppear = (void *)method_getImplementation(originalMethod); if(!class_addMethod(self, @selector(viewDidAppear:), (IMP) newViewDidAppear, method_getTypeEncoding(originalMethod))) { method_setImplementation(originalMethod, (IMP) newViewDidAppear); }}
通過 Method Swizzling 昨忆,我們成功把邏輯代碼跟處理事件記錄的代碼解耦丁频。當(dāng)然除了 Logging ,還有很多類似的事務(wù)邑贴,如 Authentication 和 Caching席里。這些事務(wù)瑣碎,跟主要業(yè)務(wù)邏輯無關(guān)拢驾,在很多地方都有胁勺,又很難抽象出來單獨的模塊。這種程序設(shè)計問題独旷,業(yè)界也給了他們一個名字 - Cross Cutting Concerns署穗。
而像上面例子用 Method Swizzling 動態(tài)給指定的方法添加代碼,以解決 Cross Cutting Concerns 的編程方式叫:Aspect Oriented Programming
Aspect Oriented Programming (面向切面編程)
Wikipedia 里對 AOP 是這么介紹的:
An aspect can alter the behavior of the base code by applying advice (additional behavior) at various join points (points in a program) specified in a quantification or query called a pointcut (that detects whether a given join point matches).
在 Objective-C 的世界里嵌洼,這句話意思就是利用 Runtime 特性給指定的方法添加自定義代碼案疲。有很多方式可以實現(xiàn) AOP ,Method Swizzling 就是其中之一麻养。而且幸運的是褐啡,目前已經(jīng)有一些第三方庫可以讓你不需要了解 Runtime ,就能直接開始使用 AOP 鳖昌。
Aspects 就是一個不錯的 AOP 庫备畦,封裝了 Runtime , Method Swizzling 這些黑色技巧许昨,只提供兩個簡單的API:
- (id<AspectToken>)aspect_hookSelector:(SEL)selector withOptions:(AspectOptions)options usingBlock:(id)block error:(NSError **)error;- (id<AspectToken>)aspect_hookSelector:(SEL)selector withOptions:(AspectOptions)options usingBlock:(id)block error:(NSError **)error;
使用 Aspects 提供的 API懂盐,我們之前的例子會進化成這個樣子:
@implementation UIViewController (Logging)+ (void)load{ [UIViewController aspect_hookSelector:@selector(viewDidAppear:) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> aspectInfo) { NSString *className = NSStringFromClass([[aspectInfo instance] class]); [Logging logWithEventName:className]; } error:NULL];}
你可以用同樣的方式在任何你感興趣的方法里添加自定義代碼,比如 IBAction 的方法里糕档。更好的方式莉恼,你提供一個 Logging 的配置文件作為唯一處理事件記錄的地方:
@implementation AppDelegate (Logging)+ (void)setupLogging{ NSDictionary *config = @{ @"MainViewController": @{ GLLoggingPageImpression: @"page imp - main page", GLLoggingTrackedEvents: @[ @{ GLLoggingEventName: @"button one clicked", GLLoggingEventSelectorName: @"buttonOneClicked:", GLLoggingEventHandlerBlock: ^(id<AspectInfo> aspectInfo) { [Logging logWithEventName:@"button one clicked"]; }, }, @{ GLLoggingEventName: @"button two clicked", GLLoggingEventSelectorName: @"buttonTwoClicked:", GLLoggingEventHandlerBlock: ^(id<AspectInfo> aspectInfo) { [Logging logWithEventName:@"button two clicked"]; }, }, ], }, @"DetailViewController": @{ GLLoggingPageImpression: @"page imp - detail page", } }; [AppDelegate setupWithConfiguration:config];}+ (void)setupWithConfiguration:(NSDictionary *)configs{ // Hook Page Impression [UIViewController aspect_hookSelector:@selector(viewDidAppear:) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> aspectInfo) { NSString *className = NSStringFromClass([[aspectInfo instance] class]); [Logging logWithEventName:className]; } error:NULL]; // Hook Events for (NSString *className in configs) { Class clazz = NSClassFromString(className); NSDictionary *config = configs[className]; if (config[GLLoggingTrackedEvents]) { for (NSDictionary *event in config[GLLoggingTrackedEvents]) { SEL selekor = NSSelectorFromString(event[GLLoggingEventSelectorName]); AspectHandlerBlock block = event[GLLoggingEventHandlerBlock]; [clazz aspect_hookSelector:selekor withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> aspectInfo) { block(aspectInfo); } error:NULL]; } } }}
然后在 -application:didFinishLaunchingWithOptions:
里調(diào)用 setupLogging
:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. [self setupLogging]; return YES;}
最后的話
利用 objective-C Runtime 特性和 Aspect Oriented Programming ,我們可以把瑣碎事務(wù)的邏輯從主邏輯中分離出來,作為單獨的模塊俐银。它是對面向?qū)ο缶幊棠J降囊粋€補充尿背。Logging 是個經(jīng)典的應(yīng)用,這里做個拋磚引玉捶惜,發(fā)揮想象力田藐,可以做出其他有趣的應(yīng)用。
使用 Aspects 完整的例子可以從這里獲得:AspectsDemo吱七。
如果你有什么問題和想法坞淮,歡迎留言或者發(fā)郵件給我 peng@glowing.com 進行討論。
Reference
method-swizzling
method replacement for fun and profit
Aspects