ObjCsingleton.h
@interface <#CLASSNAME#> : NSObject {
}
#pragma mark -
#pragma mark Class and Singleton Methods
#pragma mark -
+ (<#CLASSNAME#> *) sharedLib;
@end
ObjCsingleton.m
#import "<#CLASSNAME#>.h"
@implementation <#CLASSNAME#>
static <#CLASSNAME#> *sharedInstance = nil;
#pragma mark -
#pragma mark Class and Singleton methods
#pragma mark -
+ (<#CLASSNAME#> *)sharedLib {
@synchronized(self) {
if (sharedInstance == nil) {
sharedInstance = [[<#CLASSNAME#> alloc] init];
}
}
return sharedInstance;
}
+ (id)allocWithZone:(NSZone *)zone {
@synchronized(self) {
if (sharedInstance == nil) {
sharedInstance = [super allocWithZone:zone];
return sharedInstance; // assignment and return on first allocation
}
}
return nil; // on subsequent allocation attempts return nil
}
- (id)copyWithZone:(NSZone *)zone {
return self;
}
- (id)retain {
return self;
}
- (unsigned)retainCount {
return UINT_MAX; // denotes an object that cannot be released
}
- (void)release {
//do nothing
}
- (id)autorelease {
return self;
}
#pragma mark -
#pragma mark Init
#pragma mark -
//----------------------------------------------------------
// - (id) init
//
//----------------------------------------------------------
- (id) init {
self = [super init];
if (self) {
// do something
}
return self;
}
@end
How to use
Instantiating
I typically have initialization occur in windowDidLoad: (Cocoa) or applicationDidFinishLoading (iOS) to ensure it is instantiated early and useful at all points of the application. The sharedLib method handles initialization.
OCMySingletonObj *mySingleton = [OCMySingletonObj sharedLib];
Properties
Properties don't work the same way here because dot notation cannot be used. In order to get the value of a property, you have to call the method equivalent. So, say the above class OCConstantsLib has a public property propertyName, complete with property and synthesize declarations...
//ocConstants = [OCConstantsLib sharedLib];
NSDataType *dataName = [[OCConstantsLib sharedLib] propertyName];
That's it. No muss, no fuss.