sri
sri

Reputation: 3389

How to use global variables in Objective-C?

How should I declare a global variable in my Objective-C project?

Upvotes: 22

Views: 41526

Answers (4)

user2882399
user2882399

Reputation:

Globals rock! I don't know what everyone is scared of. I used them successfully here.

Passing Data between View Controllers

Also used UIStepper to adjust values in another viewController. I could see them being an issue is larger programs, and in my opinion the singleton thing is just a masked global. Keep it simple, if your app is simple that is.

Upvotes: 0

aepryus
aepryus

Reputation: 4825

In my experience there are few instances when a program doesn't need, at least, some sort of data or utility/helper methods that can be accessed throughout the program.

They way I deal with this, rather than using global variables is to create what I call a 'project applicance', which is essentially just a class with a bunch of static methods.

It could be implemented multiple ways, but I use a singleton and just have the static methods call through to the single instance of the appliance class. For example, in my project Oovium I have:

Oovium.h:

@interface Oovium : NSObject {
    UIWindow* _window;
}

+ (UIWindow*) window;

Oovium.m:

@implementation Oovium

static Oovium* oovium;

- (UIWindow*) window {return _window;}

+ (void) initialize {
    oovium = [[Oovium alloc] init];
}

+ (UIWindow*) window {return [oovium window];}

I then include Oovium.h in my Oovium_Prefix.pch file so that it is automatically included in all of my files.

Upvotes: 3

dreamlax
dreamlax

Reputation: 95405

Traditionally, global variables are declared in a header, and defined in a source file. Other source files only need to know how it is declared to use it (i.e. its type and its name). As long as the variable is defined somewhere in a source file, the linker will be able to find it and appropriately link all the references in other source files to the definition.

Somewhere in your header, you would declare a global variable like this:

extern int GlobalInt;

The extern part tells the compiler that this is just a declaration that an object of type int identified by GlobalInt exists. It may be defined later or it may not (it is not the compiler's responsibility to ensure it exists, that is the linker's job). It is similar to a function prototype in this regard.

In one of your source files, you define the GlobalInt integer:

int GlobalInt = 4;

Now, each file that includes the header will have access to GlobalInt, because the header says it exists, so the compiler is happy, and the linker will see it in one of your source files, so it too will be happy. Just don't define it twice!

However


You should consider whether or not this approach is useful. Global variables get messy for a number of reasons (trying to find out exactly where it is defined or declared, threading issues), there is usually not a need for global variables. You should perhaps consider using a singleton approach.

Upvotes: 35

Jonathan Grynspan
Jonathan Grynspan

Reputation: 43470

Don't. Global variables are often a sign of poor design. A common replacement in Objective-C is a class method that returns an object (that may or may not be a singleton), such as [NSUserDefaults standardUserDefaults] or [UIDevice currentDevice].

However, if you must use a global variable, read on.

In your header:

extern NSString *someString;
extern NSInteger someInteger;

In your implementation file:

NSString *someString = @"DEFAULT_VALUE";
NSInteger someInteger = DEFAULT_VALUE;

Upvotes: 16

Related Questions