I'm running into a crash when using +[NSTimer scheduledTimerWithTimeInterval:invocation:repeats]
on iOS 7. The code is straightforward enough; here is the copy paste (with variable renames) in its entirety.
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];
When the timer fires, my app crashes with the following stack trace:
I thought that maybe one of the variables was no longer retained (even though NSTimer's documentation mentions that it retains all referenced parameters), so I strongly retained all of the variables to self
. Unfortunately, the crash persists.
Thanks in advance!
You are missing this line
[self.invocation setSelector:selector];
This will work
Output: