Skip to content

Instantly share code, notes, and snippets.

@florieger
Created October 21, 2011 11:32
Show Gist options
  • Save florieger/1303617 to your computer and use it in GitHub Desktop.
Save florieger/1303617 to your computer and use it in GitHub Desktop.
Cocoa Singleton implementation
#import "ACSingleton.h"
@implementation ACSingleton
#pragma mark -
#pragma mark Singleton Pattern
static ACSingleton* _sharedInstance;
static dispatch_once_t _instanceFlag;
// Method to get the shared instance
+ (ACSingleton*)sharedInstance
{
dispatch_once(&_instanceFlag, ^{
_sharedInstance = [[self alloc] init];
});
return _sharedInstance;
}
// No copies are allowed
- (id)copyWithZone:(NSZone *)zone
{
return [ACSingleton sharedInstance];
}
#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