PDA

View Full Version : Timer functionality




Sergio10
May 23, 2009, 04:08 PM
Hi,

How to create timer? I need every second call some function(e.g. updateTimer)
I developed:

NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
...
- (void)updateTimer:(id)sender
{
NSLog(@"my text\n");
}

But it crashes. What I'm doing wrong?

Thanks.



kainjow
May 23, 2009, 04:55 PM
Use @selector(updateTimer:)

macfanboy
May 23, 2009, 09:36 PM
scheduledTimerWithTimeInterval:1.0f

why is there an f?

firewood
May 23, 2009, 11:53 PM
why is there an f?

It's C for "make this constant a single precision float", which is a complete and utter waste, since scheduledTimerWithTimeInterval takes a NSTimeInterval === a float double, so the parameter will get converted back to a float double before getting pushed on the stack for the function/method/message invocation anyway.

BlackWolf
May 24, 2009, 05:23 AM
well, in the end it doesn't really matter. you are giving the timer the selector "updateTimer" but you have the method "updateTimer:" - so your method never gets called. but, your method is pretty wrong anyway, because the reference of NSTimer says:

aSelector
The message to send to target when the timer fires.
The selector must have the following signature:

- (void)timerFireMethod:(NSTimer*)theTimer

if you need to pass additional parameters you must use
scheduledTimerWithTimeInterval:invocation:repeats: