Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

BadWolf13

macrumors 6502
Original poster
Dec 17, 2009
271
0
Ok, working though the Core Data Utility Tutorial, and there's a bit of code here that works, but I don't understand it, so I was hoping someone could explain it to me.

Code:
static NSManagedObjectModel *mom = nil;

if (mom != nil) {
	return mom;
}

What I see is that it's creating an instance of NSManagedObjectModel and assigning it a nil value. Then it's asking if that instance is not equal to nil. How can it ever NOT be nil, if the line directly before the if statement assigns it a nil value? This is what I don't understand. Anyone got any clue?
 

Catfish_Man

macrumors 68030
Sep 13, 2001
2,579
2
Portland, OR
static is what's called a "storage class". It effectively makes that a global variable; initialized to 0 at program startup time, and only one copy ever instead of a new one each time the function is called.

global variables are static by default.

<edit>
That code, by itself, is broken though. It needs to be assigned non-nil *somewhere*. The usual pattern is:
Code:
static Foo *bar;
if (bar == nil) {
    bar = [[Foo alloc] init];
}
return bar;
</edit>
 

BadWolf13

macrumors 6502
Original poster
Dec 17, 2009
271
0
So the use of the "static" at the beginning of the declaration basically causes the program to ignore the declaration and value assignment if the instance has already been declared?
 

BadWolf13

macrumors 6502
Original poster
Dec 17, 2009
271
0
Actually, static on a global variable makes it private to that source module. Remember Cocoa is basically C, not C++.

Perhaps I should have specified. This code snippet is actually within a method, so if the "static" wasn't there, it would not have been a global.
 

knightlie

macrumors 6502a
Feb 18, 2008
546
0
The pattern behind this code is that the managedObjectModel() function is called whenever your program needs a managed object model, but the function will only create mom if it doesn't already exist - it's a Singleton pattern. If the static variable mom were set to nil then the next time managedObjectModel() was called it would recreate mom.
 

Sydde

macrumors 68030
Aug 17, 2009
2,552
7,050
IOKWARDI
Perhaps I should have specified. This code snippet is actually within a method, so if the "static" wasn't there, it would not have been a global.

Correct. Local variables live on the call stack, so they are transient. Static and global variables live in a reserved space at the bottom of the heap, just above constants. Declarations made within a routine or method are variables that cannot be seen outside that routine or method.

If "static" were not there, mom would be a local variable, living on the stack. The "static" part tells the compiler to reserve heap space for mom and initialize it to nil with code placed near where the program starts, not when the routine is called.
 

BadWolf13

macrumors 6502
Original poster
Dec 17, 2009
271
0
The "static" part tells the compiler to reserve heap space for mom and initialize it to nil with code placed near where the program starts, not when the routine is called.

Wait, I may just be more confused, or I may just get it now. The "static" keyword there causes the compiler to move that line to the beginning of the program instead of at the beginning of the method?
 

ulbador

macrumors 68000
Feb 11, 2010
1,554
0
Although static variable are sort of global, it basically just means a variable or a method you can call without instantiating the class. Where I have used it the most is in Java, but it's going to be pretty similar in most languages. Take this class:

Code:
class TestClass {

public static String MyVar1 = "hello";
public string MyVar2 = "world";

public TestClass() {}

}

In this case, to get at MyVar1, you can just do:

System.out.println(TestClass.MyVar1);

Which would print out "hello". If you tried to do:

System.out.println(TestClass.MyVar2);

You would get a null pointer.

To get at MyVar2, you would actually be required to instantiate the class.

TestClass tc = new TestClass();
System.out.println(tc.MyVar2);

Which prints "world".

Of course, your mileage may vary depending on the language, but as I said, it's going to be largely the same in most high level languages. As someone above mentioned, they can be huge memory hogs, since all static variables and methods for classes have to be allocated in memory when the program begins, not on demand.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.