Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

martin chibwe
martin chibwe
3,901 Points

How do I set a date that keeps track of the previous date and updates the current date.

I am using [[NSDate alloc ] timeIntervalSince1970]; but it only keeps track of the current date when I relaunch the application it sets to the current date without recoding the previous date

4 Answers

Sorry about that

NSUserDefaults allows you to save and restore information even if your application stops running or the user restarts their device. So after you get the current date:

To save the date:

   NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
   [userDefaults setObject:[put your date object here] forKey:@"previousDate"]; //store value
   [userDefaults synchronize]; //save now

Now when you get a new date value but need to retrieve the previous one, you use:

   [userDefaults objectForKey:@"previousDate"];

You need to save the previous date before getting the new value. Perhaps try NSUserDefaults to save the date so when you open the app you still have the previous one as well as the new one.

martin chibwe
martin chibwe
3,901 Points

am a little bit new to ios could please spell it out for me

martin chibwe
martin chibwe
3,901 Points

that was helpful thank you