在方法执行前后加需要另外执行的代码,怎么加,用aspect。
- 下载aspects,放进项目中
- 直接上代码:
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; //参数
id ssss = aspectInfo.instance; //调用的实例对象
[ss invoke];//原始的方法,再次调用
// [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顾名思义应该就是“方法签名”,类似于C++中的编译器时的函数签名。苹果官方定义该类为对方法的参数、返回类似进行封装,协同NSInvocation实现消息转发。通过消息转发实现类似C++中的多重继承。
//
// iOS中的SEL,它的作用和C、C++中的函数指针很相似,通过performSelector:withObject:函数可以直接调用这个消息。但是perform相关的这些函数,有一个局限性,其参数数量不能超过2个,否则要做很麻烦的处理,与之相对,NSInvocation也是一种消息调用的方法,并且它的参数没有限制。这两种直接调用对象消息的方法,在IOS4.0之后,大多被block结构所取代,只有在很老的兼容性系统中才会使用。
[self test];
}
- (void)test {
int a = 1;
int b = 2;
int c = 3;
SEL myMethod = @selector(myLog:param:parm:);
SEL myMethod2 = @selector(myLog);
// 创建一个函数签名,这个签名可以是任意的,但需要注意,签名函数的参数数量要和调用的一致。
// 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];
// 设置target
//1.
[invocation setTarget:self];
// 设置selector
[invocation setSelector:myMethod];
// 注意:1、这里设置参数的Index 需要从2开始,因为前两个被selector和target占用。
[invocation setArgument:&a atIndex:2];
[invocation setArgument:&b atIndex:3];
[invocation setArgument:&c atIndex:4];
// [invocation retainArguments];
// 我们将c的值设置为返回值
[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);
// 消息调用
//2.
// [invocation invokeWithTarget:self];
[invocation invoke];
}
//注意:代码中用1.标识的为第一种用法,通过setTarget和setSelector来设置NSInvocation的参数,而用2.标识的是另一种用法,通过setArgument atIndex:来设置参数。看个人的喜好。。。
- (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, @"调用testCallAndExecuteBlock 必须调用 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