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;
}
}