Skip to content

Instantly share code, notes, and snippets.

@jfcpcosta
Forked from flipjorge/Singleton.h
Created December 26, 2013 21:35
Show Gist options
  • Save jfcpcosta/8138995 to your computer and use it in GitHub Desktop.
Save jfcpcosta/8138995 to your computer and use it in GitHub Desktop.
@interface Singleton
+(Singleton*)sharedSingleton;
@end
@implementation Singleton
static Singleton *_sharedSingleton;
+(Singleton*)sharedSingleton{
@synchronized([Singleton class])
{
if(!_sharedSingleton)
_sharedSingleton = [[self alloc] init];
return _sharedSingleton;
}
return nil;
}
+(id)alloc
{
@synchronized([Singleton class])
{
NSAssert(_sharedSingleton == nil, @"Attempted to allocate a second instance of the Singleton singleton");
_sharedSingleton = [super alloc];
return _sharedSingleton;
}
return nil;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment