ios - 创建一个全局变量来存储 NSDate 的时间?

标签 ios xcode global-variables nstimer nstimeinterval

我想在程序运行期间满足 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/

相关文章:

objective-c - UITableview 在 reloadData 调用时抛出 "invalid number of rows"错误,但仅适用于 iPad 1 而不是 iPad 2

c# - Unity 中出现此错误 "Download over http ,please consider updating to https"的任何永久解决方案?

ios - Xcode 显示在 CoreData 类上使用未声明的类型

c++ - 将全局声明为类声明语句的一部分与使用单独的语句声明全局之间有区别吗?

javascript - 通过 Angular Controller 中的全局变量将信息从一个函数传输到另一个函数

ios - 刷新 ios 上的重复数据

ios - 通用 UITableViewCell 标识符

iPhone 6.0 模拟器不显示主页按钮或框架

xcode - 如何在 Xcode 中手动强制索引?

java - 使用构造函数的参数作为全局变量