在我的应用我有下面的代码段:编写等效代码为iOS中9
__weak __typeof(self)weakSelf = self;
_pingTimer = [NSTimer scheduledTimerWithTimeInterval:5.0
repeats:YES
block:^(NSTimer * _Nonnull timer)
{
__strong __typeof(weakSelf)strongSelf = weakSelf;
[strongSelf pingWithBlock:nil];
}];
这完全在iOS的10+,但我需要的应用程序支持的iOS 9为好。所以我需要提供一种适用于两者的方法。
我尝试这样做:
__weak __typeof(self)weakSelf = self;
_pingTimer = [NSTimer scheduledTimerWithTimeInterval:5.0
target:weakSelf
selector:@selector(pingWithBlock:)
userInfo:nil
repeats:YES];
pingWithBlock方法是在同一个类中定义的,它是一个实例方法。
但是,这似乎不工作,这意味着我得到一个不好的内存访问崩溃。
如果有人有任何建议,将不胜感激。
编辑:下面 感谢@dgatwood解释代码修复该问题
- (void)autoPing
{
_pingTimer = [NSTimer scheduledTimerWithTimeInterval:self.autoCheckInterval
target:self
selector:@selector(pingWithBlock)
userInfo:nil
repeats:YES];
}
-(void)pingWithBlock
{
[self pingWithBlock:nil];
}
你的'pingWithBlock:'方法在哪里?并定义“不起作用”。 – rmaddy
刚刚更新了我的问题 –
不要编辑你的问题的答案。发布实际的答案。 – rmaddy