这两天升级了下,然后更改了以前的代码NSTimer,这个方法会造成循环引用。
[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(showUp) userInfo:nil repeats:NO];
所以改成了下面代码,但是发现升级后,有用户反馈崩溃了,根据友盟提供的崩溃日志找到了源头,
[NSTimer scheduledTimerWithTimeInterval:0.2 repeats:NO block:^(NSTimer * _Nonnull timer) {
}];
然后点进源码一看,才发现了错误,这是iOS10之后才支持的,所以会报错,导致崩溃。
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)repeats block:(void (^)(NSTimer *timer))block API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0));
解决办法,新建一个类继承NSProxy,实现消息转发,forwardingTargetForSelector这个方法是直接打不出来的,因为NSProxy是没有这个方法的什么,但是也同样是可以达到消息转发。
@interface MMProxy : NSProxy
+ (instancetype)proxyWithTarget:(id)target;
@end
#import "MMProxy.h"
@interface MMProxy()
@property (nonatomic, weak) id object;
@end
@implementation MMProxy
+ (instancetype)proxyWithTarget:(id)target{
MMProxy *proxy = [MMProxy alloc];
proxy.object = target;
return proxy;
}
- (id)forwardingTargetForSelector:(SEL)aSelector{
return self.object;
}
@end