I would recommend creating your own singleton class so as not to clutter up the UIApplication delegate. It also makes your code tidier. Subclass NSObject and add code similar to the following:
static Formats *_formatsSingleton = nil; + (Formats*) shared { if (_formatsSingleton == nil) { _formatsSingleton = [[Formats alloc] init]; } return _formatsSingleton; }
Add ivars and properties to this class as needed. You can set default values ββin the init method, for example.
- (id) init; { if ((self = [super init])) { _pgGlobalIntA = 42; // ivar for an int property called globalInt _pgGlobalStringB = @"hey there"; // ivar for an NSString property called globalStringB } return self; }
Then for installation and access you will use:
[[Formats shared] setGlobalIntA: 56]; NSLog(@"Global string: '%@'", [[Formats shared] globalStringB]);
The shared
class method creates an instance of the class if it does not already exist. Therefore, you do not need to worry about creating it. This will only happen the first time you try to access or set one of your global characters.
source share