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 开发者_如何学Pythonbetween.
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?
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.
self.today = [NSDate date];
The reason is because [NSDate date]
is an autoreleased object and when you use the property it will properly retain it for you.
You need to alloc today and use the self statment:
//In AppDelegate.m
today = [[NSDate alloc] init];
self.today = [NSDate date];
and not: NSDate *today = [NSDate date];
like this you are overwriting the instance variable
精彩评论