2
votes

Is there anyway to write code instead of setting a selector to method to call in NSTimer? If i want to print hello world after 5 sec i can do it like this.

[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(helloWorld:) userInfo:nil repeats:NO];

and have this function

-(void)helloWorld:(NSTimer*)aTimer {
    NSLog(@"Hello World!");
}

But instead of writeing functions for every timer you have is it possible to add NSLog(@"Hello World!") in the same line as where i create the timer?

2
Duplicate of stackoverflow.com/questions/4007023/… where you might find a suitable answer. - hotpaw2

2 Answers

2
votes

Some methods allow you to pass a code block as a parameter. Unfortunately this isn't supported for NSTimer.