The best way to create a global class is to create a Singleton Instance. The Apple description of this is here.
However Matt Gallagher has a great page on it here and this example is based on downloading his marco header file to simplify implementation.
Create a class called AppMain as normal.
Copy Matt’s SynthesizeSingleton.h file into your project.
If it is not done, modify the line:
– (void)release \
to be:
– (oneway void)release \
to stop the newer versions of xcode warning “Warning: Conflicting distributed object modifiers on return type in implementation of ‘release”
In the AppMain.h file
@interface AppMain : NSObject
{
//Global variables and objects
NSMutableArray *MyArrayName;
int MyIntName;
}
@property (nonatomic, retain) NSMutableArray *MyArrayName;
@property int MyIntName;
+ (AppMain *)sharedAppMain; //- ADD THIS
In the AppMain.m file
#import "AppMain.h"
#import "SynthesizeSingleton.h" //- ADD THIS
@implementation AppMain
@synthesize MyArrayName, MyIntName;
SYNTHESIZE_SINGLETON_FOR_CLASS(AppMain); //- ADD THIS
@end
Add Class To Prefix.pch
Add to MyAppName_Prefix.pch
#import "AppMain.h"
Accessing Variables And Objects
[AppMain sharedAppMain].MyIntName = 1234;