Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do I write a Timer in Objective-C?

I am trying to make a stop watch with NSTimer.

I gave the following code:

 nst_Timer = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(showTime) userInfo:nil repeats:NO]; 

and it is not working in milliseconds. It takes more than 1 millisecond.

like image 371
new_programmer Avatar asked Aug 19 '10 07:08

new_programmer


People also ask

How to use timer in objective c?

There are sections on "Creating and Scheduling a Timer", "Stopping a Timer" and "Memory Management". From the article, creating a scheduled, non-repeating timer can be done something like this: [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(targetMethod:) userInfo:nil repeats:NO];

What is NS timer?

A timer that fires after a certain time interval has elapsed, sending a specified message to a target object.


1 Answers

Don't use NSTimer that way. NSTimer is normally used to fire a selector at some time interval. It isn't high precision and isn't suited to what you want to do.

What you want is a High resolution timer class (using NSDate):

Output:

Total time was: 0.002027 milliseconds Total time was: 0.000002 seconds Total time was: 0.000000 minutes 

Main:

Timer *timer = [[Timer alloc] init];  [timer startTimer]; // Do some work [timer stopTimer];  NSLog(@"Total time was: %lf milliseconds", [timer timeElapsedInMilliseconds]);   NSLog(@"Total time was: %lf seconds", [timer timeElapsedInSeconds]); NSLog(@"Total time was: %lf minutes", [timer timeElapsedInMinutes]); 

Edit: Added methods for -timeElapsedInMilliseconds and -timeElapsedInMinutes

Timer.h:

#import <Foundation/Foundation.h>  @interface Timer : NSObject {     NSDate *start;     NSDate *end; }  - (void) startTimer; - (void) stopTimer; - (double) timeElapsedInSeconds; - (double) timeElapsedInMilliseconds; - (double) timeElapsedInMinutes;  @end 

Timer.m

#import "Timer.h"  @implementation Timer  - (id) init {     self = [super init];     if (self != nil) {         start = nil;         end = nil;     }     return self; }  - (void) startTimer {     start = [NSDate date]; }  - (void) stopTimer {     end = [NSDate date]; }  - (double) timeElapsedInSeconds {     return [end timeIntervalSinceDate:start]; }  - (double) timeElapsedInMilliseconds {     return [self timeElapsedInSeconds] * 1000.0f; }  - (double) timeElapsedInMinutes {     return [self timeElapsedInSeconds] / 60.0f; }  @end 
like image 164
Brock Woolf Avatar answered Sep 19 '22 20:09

Brock Woolf