Skip to content

Instantly share code, notes, and snippets.

@emarashliev
Created November 5, 2013 12:49
Show Gist options
  • Save emarashliev/7318533 to your computer and use it in GitHub Desktop.
Save emarashliev/7318533 to your computer and use it in GitHub Desktop.
Objective-c Singleton
@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