在iOS 7上使用+[NSTimer scheduledTimerWithTimeInterval:invocation:repeats]
时遇到崩溃。代码很简单;这里是完整的复制粘贴(带有变量重命名)。
SEL selector = @selector(callback);
NSMethodSignature *signature = [self methodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setTarget:self];
[NSTimer scheduledTimerWithTimeInterval:0.5 invocation:invocation repeats:NO];
当计时器触发时,我的应用程序崩溃并显示以下堆栈跟踪:
我认为可能其中一个变量不再保留(尽管NSTimer的文档提到它保留了所有引用的参数),因此我强烈保留了self
的所有变量。不幸的是,崩溃仍然存在。
提前致谢!
答案 0 :(得分:2)
您缺少此行[self.invocation setSelector:selector];
这将有效
SEL selector = @selector(callback);
NSMethodSignature *signature = [self methodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setTarget:self];
[invocation setSelector:selector];
[NSTimer scheduledTimerWithTimeInterval:0.5 invocation:invocation repeats:NO];
- (void)callback
{
NSLog(@"triggered");
}
<强>输出:强>
triggered
答案 1 :(得分:1)
This answer似乎建议您除了使用签名初始化之外,还需要在调用上调用setSelector :.