Shawn Shawn - 9 months ago 103
Objective-C Question

Global variable in Objective-C program

I want store a number as a global variable. What syntax do I use, and how can other parts of my application access that variable?

Answer Source

For a standard global variable (not persistent when the app is terminated and restarted) add this to a header file (*.h) of your choice:

extern NSInteger MYGlobalVariable;

Then put this in the implementation file; (*.m, *.c, *.cpp):

MYGlobalVariable = 0;  // Or any other default value.

That is how you do a bread and butter global variable.