Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8041003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:04:29+00:00 2026-06-05T04:04:29+00:00

I have a class called Timestamp. This class contains a static NSDate variable which

  • 0

I have a class called “Timestamp”. This class contains a static NSDate variable which is initialized the FIRST time using NSUserDefaults the app opens. This date is turned into a string which is used later. This is how I first configure date:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *string = [defaults objectForKey:@"timestampCheck"];

if (!string) { 

    Timestamp *tstamp = [[Timestamp alloc] init];
    [tstamp setSingleTimeStamp : [NSDate date]];
    [tstamp release];

    [defaults setObject:@"notFirst" forKey:@"timestampCheck"];
    [defaults synchronize];

} else {

    Timestamp *ti = [[Timestamp alloc] init];
    [ti initializeTimestamp];
    [ti release];

}

initializeTimestamp 

method is just retrieving the timestamp from the defaults. Which is set in a method called:synchronizeTimestamp

That method is called when the app enters background state.

The Timestamp class looks like this:

    - (void)setSingleTimeStamp:(NSDate *)dateProvided {

    date = dateProvided;
    [date retain];
}

- (void)synchronizeTimestamp {

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    [defaults setObject:date forKey:@"timestampDate"];

    [defaults synchronize];

}

- (void)initializeTimestamp {

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    date = [defaults objectForKey:@"timestampDate"];

}

- (NSString *)dateStringContainer {


    NSDate *tempDate = date; 

    unsigned units = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *components = [calendar components:units fromDate:tempDate];

    NSInteger year = [components year];
    NSInteger month = [components month];
    NSInteger day = [components day];

    return [NSString stringWithFormat:@"%i - %i - %i", day, month, year]; 

}

A label’s text in a separate view controller is set to dateStringContainer. Of course when I delete and build my app it shows the right date. But when I quit the app and delete from the multitasking bar the label text is:

1 - 1 - 2001

And date is nil, after some NSLog's

UPDATE:

Ok so basically, the date is working the first time the app opens ( I have to delete the app every single time to do this ), when my app is plugged in. But when I go back to it after removing it from the multitasking bar, the date is incorrect. This means that there is something wrong with my NSUserDefaults code right? Just guessing…

EDIT:

I have noticed that my synchronizeTimestamp method isn’t called. But that may be because I am stopping the app on Xcode, does stopping that app on Xcode turn it into background mode?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T04:04:30+00:00Added an answer on June 5, 2026 at 4:04 am

    You will have to retain the following

    date = [defaults objectForKey:@"timestampDate"];
    

    wich should be

    date = [[defaults objectForKey:@"timestampDate"] retain];
    

    Since the defaults will return an autoreleased object,
    I dont know if this will fix all your problems, but it is certainly a bug

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model called Category which looks like this: class Category < ActiveRecord::Base
I have a class called LocalConstants.... public static class LocalConstants { public static string
I have a class called LayoutManager. The purpose of this class is to hold
i have this class called MemoryManager, it is supposed to implement a simple smart
I have a class called Path for which there are defined about 10 methods,
I have this entity class, called Pagina and I want to update the entry
I have a model called ClassFile that takes a FileField (document): class ClassFile(models.Model): timestamp
I have a php class that tells time like this (Time.class.php): <?PHP class Time
I have a custom UserType which stores a date/time value in a TIMESTAMP field
I have class called ItemList, which is used to provide a list of suggested

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.