42

I'm looking at the following apple example source code:

    /*
 Cache the formatter. Normally you would use one of the date formatter styles (such as NSDateFormatterShortStyle), but here we want a specific format that excludes seconds.
 */
static NSDateFormatter *dateFormatter = nil;
if (dateFormatter == nil) {
    dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"h:mm a"];
}

Trying to figure out:

  • Why use the static keyword?

  • How this equates to a cached variable if you set it to nil each time the method is called.

The code is from Example 4 in the Tableview Suite demo

3 Answers 3

63

Static variables retain their assigned values over repeated calls to the function. They're basically like global values that are only "visible" to that function.

The initializer statement is only executed once however.

This code initializes dateFormatter to nil the first time the function is used. On every subsequent call to the function a check is made against value of dateFormatter. If it's not set (which will only be true the first time) a a new dateFormatter is created. If it is set then the static dateFormatter variable will be used instead.

It's worth becoming familiar with static variables. They can be very convenient but have downsides too (in this example it's impossible to release the dateFormatter object for example).

Just a tip: Sometimes it can be educational to place a breakpoint in the code and have a look to see what's going on. As the complexity of your programs increase this will become an invaluable skill.

1
  • 1
    Hi @Andrew Grant, very nice explanation, In this case , as you have mentioned that, it is impossible to release it, suppose I want to release it and assign a new value to date formatter, then how it should be done.
    – Ranjit
    Jan 18, 2013 at 11:57
16

"static" functionally means "don't evaluate the stuff on the right side of the equals sign every time through, use its previous value instead" in this case.

Use this great power with great responsibility: you run the risk of using a whole ton of memory, since these are objects that never go away. It's rarely appropriate except for in cases like this one with NSDateFormatter.

2
  • 10
    i know this is OLD question but wondering the impact of this static keyword in relation with ARC?
    – codejunkie
    Jan 13, 2012 at 21:25
  • 3
    @codejunkie statics are stored in a different location than the heap. ARC concerns retaining and releasing heap memory so that values can persist beyond stack frames. Therefore, I doubt ARC has any effect on static variables.
    – SmileBot
    Mar 14, 2014 at 20:32
1

For reference purposes, this is how I am using the static for my date formatter for using in a table view controller.

+ (NSDateFormatter *) relativeDateFormatter
{
     static NSDateFormatter *dateFormatter;
     static dispatch_once_t onceToken;
     dispatch_once(&onceToken, ^{
         //NSLog(@"Created");
         dateFormatter = [[NSDateFormatter alloc] init];
         [dateFormatter setTimeStyle:NSDateFormatterNoStyle];
         [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
         NSLocale *locale = [NSLocale currentLocale];
         [dateFormatter setLocale:locale];
         [dateFormatter setDoesRelativeDateFormatting:YES];
     });
     return dateFormatter;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.