我想在程序运行期间满足 if 条件时将时间存储到全局变量中,因此当收到新时间时应将其存储在全局变量中,
在我的 if 条件下我想检查吗?
NSDate* CurrentTime = [NSDate date];
//小于 30 秒
if ([CurrentTime timeIntervalSinceDate:TimeStoredinglobalvariable] < 30.0f)
{
//do something
//Reset the new time to
TimeStoredinglobalvariable = CurrentTime;
}
这种实现 if 条件的方式可以实现我的任务吗?以及如何获得这个全局 使用 NSdate 完成的事情?
谢谢!
最佳答案
您可以创建带有静态变量的类方法:
.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
关于ios - 创建一个全局变量来存储 NSDate 的时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10867884/