NSProxy是一个抽象类,它并没有继承NSObject,它实现了NSObject协议,可以利用NSProxy Runtime 解决循环引用的问题,接下来我们就来聊聊关于ios验证码发送次数过多?以下内容大家不妨参考一二希望能帮到您!
ios验证码发送次数过多
NSProxy是一个抽象类,它并没有继承NSObject,它实现了NSObject协议,可以利用NSProxy Runtime 解决循环引用的问题
其实现如下:
#import "HCQProxy.h"@interface HCQProxy()@property(nonatomic, weak)NSObject* target;@end@implementation HCQProxy#pragma mark - NSProxy 没有初始化方法,需要手动实现- (instancetype)initWithTarget:(NSObject*)target { _target = target; return self;} (instancetype)proxyWithTarget:(id)target { HCQProxy* proxy = [[HCQProxy alloc]initWithTarget:target]; return proxy;}#pragma mark - 获得方法的方法签名-(NSMethodSignature*)methodSignatureForSelector:(SEL)sel { NSMethodSignature* signature = (_target) ? [_target methodSignatureForSelector:sel] : [super methodSignatureForSelector:sel] ; return signature;}#pragma mark - 为调用设置目标-(void)forwardInvocation:(NSInvocation *)invocation { if(!_target) return; [invocation invokeWithTarget:_target];}@end
写了例子验证一下
#import "UserViewController.h"@import HCQProxy.HCQProxy;@interface UserViewController ()@property(nonatomic,strong)NSTimer* timer;@endstatic int count;@implementation UserViewController- (void)viewDidLoad { [super viewDidLoad]; self.titleS = @"用户中心"; self.leftBtnClickHandler = ^(id _Nullable target) { UserViewController* vc = (UserViewController*)target; [vc.navigationController popViewControllerAnimated:YES]; };}-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:[HCQProxy proxyWithTarget:self] selector:@selector(doSomething) userInfo:nil repeats:YES];}-(void)doSomething { NSLog(@"count = %d",count );}-(void)dealloc { //NSLog(@"%s %@",__func__,_timer); if(_timer) { [_timer invalidate]; _timer = nil; } //NSLog(@"%s %@",__func__,_timer);}@end