在方法執(zhí)行前后加需要另外執(zhí)行的代碼辫封,怎么加,用aspect廉丽。
w01.png
- 下載aspects倦微,放進(jìn)項目中
- 直接上代碼:
viewController中:
#import "ViewController.h"
#import "Aspects.h"
#import "TestClass.h"
@interface ViewController ()
@property (nonatomic, strong) UIView *seView;
@property (nonatomic, copy) NSString *numStr;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// self.seView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
// [self.view addSubview:self.seView];
//
//
// // Do any additional setup after loading the view, typically from a nib.
// [UIViewController aspect_hookSelector:@selector(viewWillAppear:) withOptions:AspectPositionBefore usingBlock:^(id<AspectInfo> aspectInfo, BOOL animated) {
// NSLog(@"控制器 %@ 將要顯示: %tu", aspectInfo.instance, animated);
//
// self.seView.backgroundColor = [UIColor orangeColor];
// } error:NULL];
//
// self.seView.backgroundColor = [UIColor grayColor];
// __block BOOL isTT = NO;
// NSLog(@"wgj:%ld",re);
// NSLog(@"%ld",isTT);
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 100, 100, 50)];
button.backgroundColor = [UIColor orangeColor];
[button setTitle:@"Button" forState:(UIControlStateNormal)];
[button addTarget:self action:@selector(clickBtn) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button];
__weak typeof(self) weakSelf = self;
[TestClass aspect_hookSelector:@selector(getTheNum:) withOptions:AspectPositionBefore usingBlock:^(id<AspectInfo> aspectInfo, NSString *num){
id ss = aspectInfo.originalInvocation;//原始的方法
id sss = aspectInfo.arguments; //參數(shù)
id ssss = aspectInfo.instance; //調(diào)用的實例對象
[ss invoke];//原始的方法,再次調(diào)用
// [aspectInfo.instance isBeingDismissed];
weakSelf.numStr = @"5";
// NSString *ss = @"34";
// num = ss;
NSInteger sss2 = [TestClass getTheNum:2];
NSLog(@"%@",num);
}error:NULL];
}
- (void)clickBtn
{
// =====acpects舉例使用
// TestClass *tt = [[TestClass alloc] init];
// self.numStr = @"2";
//
// NSInteger re = [tt getTheNum:self.numStr];
//
// NSLog(@"sss:%ld",re);
//============================
// NSMethodSignature顧名思義應(yīng)該就是“方法簽名”正压,類似于C++中的編譯器時的函數(shù)簽名欣福。蘋果官方定義該類為對方法的參數(shù)、返回類似進(jìn)行封裝焦履,協(xié)同NSInvocation實現(xiàn)消息轉(zhuǎn)發(fā)拓劝。通過消息轉(zhuǎn)發(fā)實現(xiàn)類似C++中的多重繼承。
//
// iOS中的SEL嘉裤,它的作用和C郑临、C++中的函數(shù)指針很相似,通過performSelector:withObject:函數(shù)可以直接調(diào)用這個消息屑宠。但是perform相關(guān)的這些函數(shù)厢洞,有一個局限性,其參數(shù)數(shù)量不能超過2個典奉,否則要做很麻煩的處理躺翻,與之相對,NSInvocation也是一種消息調(diào)用的方法卫玖,并且它的參數(shù)沒有限制公你。這兩種直接調(diào)用對象消息的方法,在IOS4.0之后假瞬,大多被block結(jié)構(gòu)所取代省店,只有在很老的兼容性系統(tǒng)中才會使用。
[self test];
}
- (void)test {
int a = 1;
int b = 2;
int c = 3;
SEL myMethod = @selector(myLog:param:parm:);
SEL myMethod2 = @selector(myLog);
// 創(chuàng)建一個函數(shù)簽名笨触,這個簽名可以是任意的懦傍,但需要注意,簽名函數(shù)的參數(shù)數(shù)量要和調(diào)用的一致芦劣。
// NSMethodSignature *sig = [[self class] instanceMethodSignatureForSelector:myMethod];
//或者:
NSMethodSignature *sig = [NSMethodSignature signatureWithObjCTypes:"i@:iii"];
// 通過簽名初始化
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig];
// 2.FirstViewController *view = self;
// 2.[invocation setArgument:&view atIndex:0];
// 2.[invocation setArgument:&myMethod2 atIndex:1];
// 設(shè)置target
//1.
[invocation setTarget:self];
// 設(shè)置selector
[invocation setSelector:myMethod];
// 注意:1粗俱、這里設(shè)置參數(shù)的Index 需要從2開始,因為前兩個被selector和target占用虚吟。
[invocation setArgument:&a atIndex:2];
[invocation setArgument:&b atIndex:3];
[invocation setArgument:&c atIndex:4];
// [invocation retainArguments];
// 我們將c的值設(shè)置為返回值
[invocation setReturnValue:&c];
int d;
// 取這個返回值
[invocation getReturnValue:&d];
NSLog(@"d:%d", d);
NSUInteger argCount = [sig numberOfArguments];
NSLog(@"argCount:%ld", argCount);
for (NSInteger i=0; i<argCount; i++) {
NSLog(@"%s", [sig getArgumentTypeAtIndex:i]);
}
NSLog(@"returnType:%s ,returnLen:%ld", [sig methodReturnType], [sig methodReturnLength]);
NSLog(@"signature:%@" , sig);
// 消息調(diào)用
//2.
// [invocation invokeWithTarget:self];
[invocation invoke];
}
//注意:代碼中用1.標(biāo)識的為第一種用法寸认,通過setTarget和setSelector來設(shè)置NSInvocation的參數(shù)签财,而用2.標(biāo)識的是另一種用法,通過setArgument atIndex:來設(shè)置參數(shù)偏塞〕簦看個人的喜好。灸叼。神汹。
- (int)myLog:(int)a param:(int)b parm:(int)c
{
NSLog(@"MyLog:%d,%d,%d", a, b, c);
return a+b+c;
}
- (void)myLog
{
NSLog(@"你好,South China University of Technology");
}
//- (void)viewWillAppear:(BOOL)animated
//{
// [super viewWillAppear:animated];
//
// self.seView.backgroundColor = [UIColor grayColor];
//}
//- (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");
//}
@end
分類中:
#import "UIViewController+wgjTT.h"
@implementation UIViewController (wgjTT)
// 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
#import <Foundation/Foundation.h>
@interface TestClass : NSObject
- (NSInteger)getTheNum:(NSString *)numStr;
+ (NSInteger)getTheNum:(NSInteger)num;
@end
#import "TestClass.h"
@implementation TestClass
- (NSInteger)getTheNum:(NSString *)numStr
{
return [numStr intValue] + 10;
}
+ (NSInteger)getTheNum:(NSInteger)num
{
return num + 10;
}
@end