I want to store the time during the run of the program to a global variable when it satisfy a if condition, So when new time is received it should be stored in the global variable,
Here in my if condition i want to check?
NSDate* CurrentTime = [NSDate date];
// less than 30 seconds
if ([CurrentTime timeIntervalSinceDate:TimeStoredinglobalvariable] < 30.0f)
{
//do something
//Reset the new time to
TimeStoredinglobalvariable = CurrentTime;
}
Is this way of implementing a if condition possible to implement my task? And how to get this global thing done with NSdate ?
Thank you!
You could create an class method with a static variable:
.h
@interface YourClass : NSObject
@property (nonatomic, strong, readonly) NSDate *storedTime;
@end
.m
#import "YourClass.h"
@interface YourClass()
@property (nonatomic, strong, readwrite) NSDate *storedTime;
@end
@implementation YourClass
static NSDate *_storedTime;
@synthesize storedTime;
- (NSDate *)storedTime {
if (!_storedTime) {
_storedTime = [NSDate date];
}
if ([_storedTime timeIntervalSinceNow] < -30.0f) {
//Do you stuff...
_storedTime = [NSDate date];
}
return _storedTime;
}
@end