Created
November 5, 2013 12:49
-
-
Save emarashliev/7318533 to your computer and use it in GitHub Desktop.
Objective-c Singleton
This file contains 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
@implementation Singleton | |
static id _sharedManager; | |
+ (instancetype)sharedManager | |
{ | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
_sharedManager = [[self alloc] init]; | |
}); | |
return _sharedManager; | |
} | |
+ (id)alloc | |
{ | |
@synchronized(self){ | |
NSAssert(_sharedManager == nil, @"Attempt to allocate a second instance of singleton %@", [self class]); | |
_sharedManager = [super alloc]; | |
return _sharedManager; | |
} | |
return nil; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment