views:

134

answers:

4

I'm not really sure what this is doing. Is dateFormatter only settable the first time?

static NSDateFormatter *dateFormatter = nil;

if (dateFormatter == nil) {
    dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
} 

Normally I would read that to mean, set something to nil, then check if it's nil, but if I NSLog within the condtional, it only gets called once?

Extra points if you can explain static in more depth, i know it creates a global variable (?), but thats about it.

A: 

Static means that it is a global variable, however in this case it can be accessed only while in the function, the static block will happen once. The rest will happen every time but because there is a check whether it is already initialize, the block in the "if" will also happen once. So the only piece of code that will run more than once is check in the "if" condition.

Guy Ephraim
It's not really part of the class, so much as a C global for that file (or more likely the function/method it is in). The effect is the same however.
Kendall Helmstetter Gelner
What is, 'the rat'?
Onedayitwillmake
Typo, I meant "the rest of the code", fixed
Guy Ephraim
I see. Very nice answer. So that line, even though it's in the function belongs to the class?
Onedayitwillmake
Yeah, but accecible only from this function, Well as other said, it is not really part of the class but some kind of private global, but as a concept, it can be thought of part of the class
Guy Ephraim
-1 no static does not mean it's part of the class.
JeremyP
Revised the answer to accommodate the right terms
Guy Ephraim
This is the kind of question for me that seems to beget more questions. Why if it's a global variable, that is accessible only in that function does it not get called each time the function is called?
Onedayitwillmake
Global means that it lives in the context of the program and not in the context of the instance. It doesn't mean that it is accesible from anywhere.
Guy Ephraim
Removed my down vote following the edit
JeremyP
A: 

Is dateFormatter only settable the first time?

No, you can setup your format and use then use the method stringFromDate:(NSDate*) aDate to convert your date to a string with your format.

Jonathan
I think you missunderstood my question, or i worded incorrectly.
Onedayitwillmake
+1  A: 

Like Michal said, "static" is part of standard C. It has two equally important effects:

  1. "static" makes the value of a variable persist even after the variable has gone out of scope.
  2. "static" makes a variable only visible within its translation unit (where a "translation unit" is any non-header source file that has been preprocessed). If you have two global variables or constants with the same name in different translation units, one way to avoid a linking-time name collision is to make both variables static.

See the excellent Wikipedia article on "static variable".

Jon Rodriguez
+6  A: 

No, static will not make dateFormatter part of the class. It might look like that, but static is not Objective-C's feature and knows nothing about classes.

static is a standard C language's feature. Remember that Objective-C is just an extension to C. If static is used within a method, it will create a global variable visible only from within that method.

That means that this variable is not allocated on the stack but in the data segment. Variables locally defined in methods (non-static ones) are placed on the stack together with address of code where to return after method call is finished - therefore when execution leaves the method, local method's variables are gone. Within this method, dateFormatter always represents the same place in memory.

The point of making dateFormatter static in this case is improving the performance - you always format dates with the same formatter, so it doesn't make sense to recreate that formatter each time, therefore it is created and saved into a global variable only once.

Michal