Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
NSTimer+;NSInvocation导致iOS 7崩溃_Ios_Objective C_Crash_Nstimer_Nsinvocation - Fatal编程技术网

NSTimer+;NSInvocation导致iOS 7崩溃

NSTimer+;NSInvocation导致iOS 7崩溃,ios,objective-c,crash,nstimer,nsinvocation,Ios,Objective C,Crash,Nstimer,Nsinvocation,在iOS 7上使用+[NSTimer scheduledTimerWithTimeInterval:invocation:repeats]时,我遇到了崩溃。代码非常简单;这是完整的复制粘贴(带有变量重命名) SEL selector = @selector(callback); NSMethodSignature *signature = [self methodSignatureForSelector:selector]; NSInvocation *invocation = [NSInvoc

在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
的所有变量。不幸的是,崩溃依然存在


提前谢谢

您缺少这一行
[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

似乎建议您需要在调用时调用setSelector:以使用签名初始化它。

欢迎使用SO!通常,让OP知道他的问题是否重复是一种常见的礼貌(如果某个地方没有规定的话)。你发布的答案应该是你自己的。这样,如果你链接的答案有效,他就可以投票给正确的人。OP代表什么?我该怎么让她知道?对问题有何评论?OP是原创帖子(或海报)。你是对的,对这个问题的评论。我不是想阻止你回答,甚至不是告诉你删除这个答案。只是一个关于回答问题的指针:)感谢它-我一直使用它,因为它已经存在,但这是我第一次发布任何内容:)玩得好,双方。谢谢!这很有效。我应该更仔细地阅读文档。:)