Accessorizer will generate a singleton for you with overrides and locking
static QVNeuralCenter *sharedInstance = nil;
+ (void) initialize
{
if (sharedInstance == nil)
sharedInstance = [[self alloc] init];
}
+ (id) sharedQVNeuralCenter
{
//Already set by +initialize.
return sharedInstance;
}
+ (id) allocWithZone: (NSZone *) zone
{
//Usually already set by +initialize.
@synchronized(self) {
if (sharedInstance) {
//The caller expects to receive a new object, so implicitly retain it
//to balance out the eventual release message.
return [sharedInstance retain];
} else {
//When not already set, +initialize is our caller.
//It's creating the shared instance, let this go through.
return [super allocWithZone: zone];
}
}
}
- (id) init
{
//If sharedInstance is nil, +initialize is our caller, so initialze the instance.
//If it is not nil, simply return the instance without re-initializing it.
if (sharedInstance == nil) {
if ((self = [super init])) {
//Initialize the instance here.
}
}
return self;
}
- (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;
}
Singleton
Based on Peter Hosey’s article “Doing it Wrong” link






Features

only $15