Skip to content

Instantly share code, notes, and snippets.

@florieger
Created October 14, 2011 09:29
Show Gist options
  • Save florieger/1286674 to your computer and use it in GitHub Desktop.
Save florieger/1286674 to your computer and use it in GitHub Desktop.
Cocoa Singleton implementation [MRC]
#import "ACSingleton.h"
@implementation ACSingleton
#pragma mark -
#pragma mark Singelton Pattern
static ACSingleton.h* _sharedInstance;
// Method to get the shared instance
+ (id)sharedInstance
{
if (_sharedInstance == nil) {
_sharedInstance = [[super allocWithZone:NULL] init];
}
return _sharedInstance;
}
// Strict singelton implementation,
// sharedInstance method is also called when using an allocation method
+ (id)allocWithZone:(NSZone *)zone
{
return [[self sharedInstance] retain];
}
// No copies are allowed
- (id)copyWithZone:(NSZone *)zone
{
return self;
}
// Object will never be released
- (void)release
{
//do nothing
}
// No multiple retains
- (id)retain
{
return self;
}
// Autorelease does nothing
- (id)autorelease
{
return self;
}
// Retain count is never zero
- (NSUInteger)retainCount
{
return NSUIntegerMax; //denotes an object that cannot be released
}
#pragma mark -
#pragma mark Initialization
// Init methode to override by subclass
- (id)init
{
// Prevent class from being initialized multiple times
if (_sharedInstance) return _sharedInstance;
self = [super init];
if (self) {
// Initialize iVars
}
return self;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment