How to implement Globals in Objective-C – Updated

– Oh, the embarrassment of premature technical wisdom ejaculation.

– Since I wrote all the stuff, below, I’ve had a big rethink about Globals and realized that the method of implementing them that I’d adopted was seriouly lame.

– I went back and thought through how I’d done it before in the Microsoft Win32 OOP world and I reasoned my way through to doing the same thing here, and, it is sooooo much simpler and way faster.  That’s what I get for cook-booking off someone else’s code without understanding it.

– It still baffles me, when you go searching for it, why the information out there about how to implement Globals in Objective-C is such a dog’s breakfast.

– But, I know how to do it now and I’m going to write it up here and maybe that will aid someone else on a search to answer the same question.

– So, below, you will find my new method and code and the old stuff is gone.

-dennis

*—————————————————–*

Create a class, UtlGen.  In it we put general utility methods that we can call from anywhere.

Create a file, globals.h, where we can declare our globals variables.  In it I have the following line:

UtlGen * g_pUtl;

This creates a pointer to an instance of the UtlGen class.

I want this pointer to be (globally) visible from anywhere in my program so I need to place it outside of any blocks.  I’d also like to put in a place that makes sense to other programmers.  To me, there are two obvious choices.  One is in the main.m file and the other is in the AppDelegate.m file.  Main.m is the first place where execution begin and the AppDelegate.m is the second.   I select the AppDelegate.m file because I think folks don’t generally expect you to mess with the main.m file.

Near the top of the AppDelegate.m file, I import the global.h file like so:

#import “myprogramAppDelegate.h”
#import “global.h”   <— global variables live here.
@implementation myprogramAppDelegate

We also instantiate and release the UtlGen object in the AppDelegate. module.   The very first method called in AppDelegate.m is:

-(BOOL) application( … and so on …

Inside this method, we add the line:

g_pUtl = [[UtlGen alloc] init]; 

This is how we instantiate the UtlGen object and assign its pointer to the global variable, g_pUtl.

In the -(void) dealloc method of the AppDelegate module, we add the following line:

[UtlGen release];

And that’s how we release the object’s memory when the app shuts down.

In the myprogram-Prefix.pch file, I place the following line in with the other imports:

#import “UtlGen.h”

This file contains the precompiled definitions that are visible to all modules.  Importing UtlGen.h here means that the methods (and their prototypes) available in the UtlGen object will be visible everywhere.

Now, we are ready to use any of the methods in the UtlGen object from any module.

Let’s use a hypothetical method called ‘twiceMe’ in the UtlGen object.

In the application, we have a class, someClass, which is implemented in the someClass.m file.  In this file is the implementation of a method, ‘doMath’.  In the doMath method, we will call the twiceMe method of the UtlGen object to double the value of an integer we pass to it.

In the someClass.m file we do this:

@implementation someClass
extern UtlGen * g_pUtl;

-(void) doMath
{
int n = [g_pUtl twiceMe:2];

The extern tells the compiler that the g_pUtl variable has been defined elsewhere (over in AppDelegate, remember?).  In doMath, we call the twiceMe property of the UtlGen object and pass it a ‘2’ and it returns a ‘4’ to us which we place into ‘n’.

That’s it.

 *——————————————————*

Comments are closed.