将全局本地时间变量存储在iphone的数据库中
我一直在尝试以 NSDATE 的形式存储全局变量,但似乎总是给出错误。我试图在应用程序开始时获取当前日期,然后在应用程序完成后将该日期存储在数据库中。 下载当前日期的原因:下载数据可能需要一些时间,并且中间会丢失一些秒数。
在 AppDelegate.h
NSDate *today;
@property (nonatomic, retain) NSDate *today;
在 AppDelegate.m
today = [NSDate date];
当我在视图控制器中查看今天的日期时,它停止运行。
在 ViewController.m 中
AppDelegate *appDelegate= [[UIApplication sharedApplication] delegate];
[appDelegate today]; // Error here
从委托方法检索日期变量的正确方法是什么?
I have been trying to store global variables in form of NSDATE but it seems it always gives error. I am trying to grab the current date at the start of the application and then store that date once the application is finished in the database.
Reason to download the current date: Downloading data which may take some time and will lose some seconds in between.
In AppDelegate.h
NSDate *today;
@property (nonatomic, retain) NSDate *today;
In AppDelegate.m
today = [NSDate date];
When I view the date today in view controller, it stops functioning.
In ViewController.m
AppDelegate *appDelegate= [[UIApplication sharedApplication] delegate];
[appDelegate today]; // Error here
What is the correct way in retrieving the date variable from delegate method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的代码示例中,您今天似乎正在重新定义。但无论哪种方式,今天都不应该直接分配,您需要像这样设置属性。
原因是因为
[NSDate date]
是一个自动释放的对象,当您使用该属性时,它会正确地为您保留它。In your code example it looks like you are redefining today. But you should not be assigned today directly either way, you need to set the property like this.
The reason is because
[NSDate date]
is an autoreleased object and when you use the property it will properly retain it for you.您需要分配today并使用自我声明:
而不是:
NSDate *today = [NSDate date];
这样您将覆盖实例变量You need to alloc today and use the self statment:
and not:
NSDate *today = [NSDate date];
like this you are overwriting the instance variable