Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Sergio10

macrumors regular
Original poster
Oct 3, 2007
137
0
Hi,

How to create timer? I need every second call some function(e.g. updateTimer)
I developed:
PHP:
	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.
 
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.
 
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:
Code:
    - (void)timerFireMethod:(NSTimer*)theTimer
if you need to pass additional parameters you must use
Code:
scheduledTimerWithTimeInterval:invocation:repeats:
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.