一個簡潔高效的用于使iOS支持AOP面向切面編程的庫.它可以幫助你在不改變一個類或類實例的代碼的前提下,有效更改類的行為.比iOS傳統(tǒng)的 AOP方法,更加簡單高效.支持在方法執(zhí)行的前/后或替代原方法執(zhí)行.曾經(jīng)是 PSPDFKit 的一部分,PSPDFKit,在Dropbox和Evernote中都有應(yīng)用,現(xiàn)在單獨單獨開源出來給大家使用.
項目主頁: aspects
最新實例:點擊下載
注: AOP是一種完全不同于OOP的設(shè)計模式.更多信息,可以參考這里: AOP 百度百科
pod "Aspects"
把文件 Aspects.h/m 拖到工程中即可.
Aspects 用于支持AOP(面向切面編程)模式,用于部分解決OOP(面向?qū)ο?模式無法解決的特定問題.具體指的是那些在多個方法有交叉,無法或很難被有效歸類的操作,比如:
Aspects 給 NSObject 擴展了下面的方法
:
/// 為一個指定的類的某個方法執(zhí)行前/替換/后,添加一段代碼塊.對這個類的所有對象都會起作用.////// @param block 方法被添加鉤子時,Aspectes會拷貝方法的簽名信息./// 第一個參數(shù)將會是 `id<AspectInfo>`,余下的參數(shù)是此被調(diào)用的方法的參數(shù)./// 這些參數(shù)是可選的,并將被用于傳遞給block代碼塊對應(yīng)位置的參數(shù)./// 你甚至使用一個沒有任何參數(shù)或只有一個`id<AspectInfo>`參數(shù)的block代碼塊.////// @注意 不支持給靜態(tài)方法添加鉤子./// @return 返回一個唯一值,用于取消此鉤子.+ (id<AspectToken>)aspect_hookSelector:(SEL)selector withOptions:(AspectOptions)options usingBlock:(id)block error:(NSError **)error;/// 為一個指定的對象的某個方法執(zhí)行前/替換/后,添加一段代碼塊.只作用于當前對象. - (id<AspectToken>)aspect_hookSelector:(SEL)selector withOptions:(AspectOptions)options usingBlock:(id)block error:(NSError **)error; /// 撤銷一個Aspect 鉤子./// @return YES 撤銷成功, 否則返回 NO. id<AspectToken> aspect = ...; [aspect remove];
所有的調(diào)用,都會是線程安全的.Aspects 使用了Objective-C 的消息轉(zhuǎn)發(fā)機會,會有一定的性能消耗.所有對于過于頻繁的調(diào)用,不建議使用 Aspects.Aspects更適用于視圖/控制器相關(guān)的等每秒調(diào)用不超過1000次的代碼.
可以在調(diào)試應(yīng)用時,使用Aspects動態(tài)添加日志記錄功能.
[UIViewController aspect_hookSelector:@selector(viewWillAppear:) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> aspectInfo, BOOL animated) { NSLog(@"控制器 %@ 將要顯示: %tu", aspectInfo.instance, animated);} error:NULL];
使用它,分析功能的設(shè)置會很簡單:
https://github.com/orta/ARAnalytics
你可以在你的測試用例中用它來檢查某個方法是否被真正調(diào)用(當涉及到繼承或類目擴展時,很容易發(fā)生某個父類/子類方法未按預(yù)期調(diào)用的情況):
- (void)testExample { TestClass *testClass = [TestClass new]; TestClass *testClass2 = [TestClass new]; __block BOOL testCallCalled = NO; [testClass aspect_hookSelector:@selector(testCall) withOptions:AspectPositionAfter usingBlock:^{ testCallCalled = YES; } error:NULL]; [testClass2 testCallAndExecuteBlock:^{ [testClass testCall]; } error:NULL]; XCTAssertTrue(testCallCalled, @"調(diào)用testCallAndExecuteBlock 必須調(diào)用 testCall");}
它對調(diào)試應(yīng)用真的會提供很大的作用.這里我想要知道究竟何時輕擊手勢的狀態(tài)發(fā)生變化(如果是某個你自定義的手勢的子類,你可以重寫setState:方法來達到類似的效果;但這里的真正目的是,捕捉所有的各類控件的輕擊手勢,以準確分析原因):
[_singleTapGesture aspect_hookSelector:@selector(setState:) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> aspectInfo) { NSLog(@"%@: %@", aspectInfo.instance, aspectInfo.arguments);} error:NULL];
下面是一個你監(jiān)測一個模態(tài)顯示的控制器何時消失的示例.通常,你也可以寫一個子類,來實現(xiàn)相似的效果,但使用 Aspects 可以有效減小你的代碼量:
@implementation UIViewController (DismissActionHook)// Will add a dismiss action once the controller gets dismissed.- (void)pspdf_addWillDismissAction:(void (^)(void))action { PSPDFAssert(action != NULL); [self aspect_hookSelector:@selector(viewWillDisappear:) withOptions:AspectPositionAfter usingBlock:^(id<AspectInfo> aspectInfo) { if ([aspectInfo.instance isBeingDismissed]) { action(); } } error:NULL];}@end
Aspectes 會自動標記自己,所有很容易在調(diào)用棧中查看某個方法是否已經(jīng)調(diào)用:
你可以使用 NSInvocation 對象類自定義返回值:
[PSPDFDrawView aspect_hookSelector:@selector(shouldPRocessTouches:withEvent:) withOptions:AspectPositionInstead usingBlock:^(id<AspectInfo> info, NSSet *touches, UIEvent *event) { // 調(diào)用方法原來的實現(xiàn). BOOL processTouches; NSInvocation *invocation = info.originalInvocation; [invocation invoke]; [invocation getReturnValue:&processTouches]; if (processTouches) { processTouches = pspdf_stylusShouldProcessTouches(touches, event); [invocation setReturnValue:&processTouches]; } } error:NULL];
兼容性與限制
新聞熱點
疑難解答