Created
October 21, 2011 11:32
-
-
Save florieger/1303617 to your computer and use it in GitHub Desktop.
Cocoa Singleton implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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