1

My utility app will have 20 individual timers, laid out like this:

- (void)updateTimer
{
NSDate *currentDate = [NSDate date];
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
NSString *timeString=[dateFormatter stringFromDate:timerDate];
stopWatchLabel.text = timeString;
[dateFormatter release];
}

- (IBAction)onStartPressed:(id)sender {
startDate = [[NSDate date]retain];

// Create the stop watch timer that fires every 1 s
stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                  target:self
                                                selector:@selector(updateTimer)
                                                userInfo:nil
                                                 repeats:YES];
}

- (IBAction)onStopPressed:(id)sender {
[stopWatchTimer invalidate];
stopWatchTimer = nil;
[self updateTimer];
}

I would like to add all of the time intervals together, and display them as a string. I thought this would be easy, but I just can't get it. I need to sum the NSTimeIntervals, right?

Joel Wehr
  • 65
  • 8
  • When you say you want to add all the time intervals together and display as a string, do you mean the time intervals from each of the 20 timers? Or is the issue getting the time interval since you started one of the timers? If for the latter, then just format `timeInterval` such as `String timeString = [NSString stringWithFormat:@"%f", timeInterval];` I don't think you need all the date formatting code. – bobnoble Nov 04 '12 at 03:31
  • To get mm:ss, use modulo 60 to get the minutes, and the remainder is seconds. – bobnoble Nov 04 '12 at 03:37
  • Yes, I need to add the time intervals from any of the 20 timers that the user needs to use. It could be one or some, or all. I want the total time of each of the timers after they are stopped. There will be separate code for each timer. – Joel Wehr Nov 04 '12 at 04:10

1 Answers1

0

Several approaches you could take. One is to create a timer class that that can be queried for its state (running, stopped, not started, ...) and for its current time interval. Add all of the timers to a collections such as an NSMutableArray. You could then iterate over all the timers in the collection, and for those that are stopped, get its time interval, and sum them together. The partial header for a MyTimer class:

enum TimerState {
    Uninitialized,
    Reset,
    Running,
    Stopped
};

typedef enum TimerState TimerState;

#import <Foundation/Foundation.h>

@interface MyTimer : NSObject

@property (nonatomic) NSTimeInterval timeInterval;
@property (nonatomic) TimerState state;

- (void) reset;
- (void) start;

@end 

Declare your array:

#define MAX_TIMER_COUNT 20
NSMutableArray *myTimerArray = [NSMutableArray arrayWithCapacity:MAX_TIMER_COUNT];

Add each timer to the array:

MyTimer *myTimer = [[MyTimer alloc] init];
[myTimerArray addObject:myTimer];

When appropriate, iterate over the collection of timers and sum the time intervals for the Stopped timers:

NSTimeInterval totalTimeInterval = 0.0;
for (MyTimer *timer in myTimerArray){
    if (timer.state == Stopped) {
        totalTimeInterval += timer.timeInterval;
    }
}
bobnoble
  • 5,794
  • 3
  • 25
  • 32
  • Thanks. I did find a similar way to do this, but your solution looks more eloquent. I'm going to mark this as answered. – Joel Wehr Nov 09 '12 at 14:46