#方法一邑商、self _>timer timer弱引用self 這么能決的解方案夏漱;
__weak typeof(self) WeakSelf = self;
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
[WeakSelf timerTest];
}];
#方法二、1屿衅、通過中間對象(中間對象弱引用)來解決
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:[HYTimerProxy proxyWithTarget:self] selector:@selector(timerTest) userInfo:nil repeats:YES];
@interface HYTimerProxy : NSObject
+(instancetype)proxyWithTarget:(id)target;
@property(nonatomic,weak)id target;
@end
@implementation HYTimerProxy
+(instancetype)proxyWithTarget:(id)target{
HYTimerProxy * proxy = [[HYTimerProxy alloc]init];
proxy.target = target;
return proxy;
}
消息轉(zhuǎn)發(fā)
- (id)forwardingTargetForSelector:(SEL)aSelector{
return self.target;
}
#方法二 2、 NSPorxy
//速度更快
@interface HYTimerProxy : NSProxy
+(instancetype)proxyWithTarget:(id)target;
@property(nonatomic,weak)id target;
@implementation HYTimerProxy
+(instancetype)proxyWithTarget:(id)target{
//NSProxy 不需要init方法挣轨、因?yàn)樗旧頉]有init方法
HYTimerProxy * proxy = [HYTimerProxy alloc];
proxy.target = target;
return proxy;
}
放回方法簽名
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector{
return [self.target methodSignatureForSelector:aSelector];
}
直接調(diào)用
- (void)forwardInvocation:(NSInvocation *)anInvocation{
[anInvocation invokeWithTarget:self.target];
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者