I have a timer calling a method but this method takes one paramether:
theTimer = [NSTimer scheduledTimerWithTimeInterval:animationInterval target:self selector:@selector(timer) userInfo:nil repeats:YES];
should be
theTimer = [NSTimer scheduledTimerWithTimeInterval:animationInterval target:self selector:@selector(timer:game) userInfo:nil repeats:YES];
now this syntax doesn't seems to be right. I tried with NSInvocation but I got some problems:
timerInvocation = [NSInvocation invocationWithMethodSignature:
[self methodSignatureForSelector:@selector(timer:game)]];
theTimer = [NSTimer scheduledTimerWithTimeInterval:animationInterval
invocation:timerInvocation
repeats:YES];
How should I use Invocation?
As @DarkDust points out,
NSTimer
expects its target method to have a particular signature. If for some reason you can't conform to that, you can instead use anNSInvocation
as you suggest, but in that case you need to fully initialise it with the selector, target and arguments. Eg:Calling
invocationWithMethodSignature
on its own doesn't do all that, it just creates an object that is able to be filled in in the right manner.You can pass
NSDictionary
with named objects (likemyParamName => myObject
) throughuserInfo
parameter like thisThen in
timer:
method:Given this definition:
You then need to use
@selector(timerFired:)
(that's the method name without any spaces or argument names, but including the colons). The object you want to pass (game
?) is passed via theuserInfo:
part:In your timer method, you can then access this object via the timer object's
userInfo
method: