Skip to content

Instantly share code, notes, and snippets.

@flipjorge
Created June 8, 2012 21:05
Show Gist options
  • Save flipjorge/2898116 to your computer and use it in GitHub Desktop.
Save flipjorge/2898116 to your computer and use it in GitHub Desktop.
iOS Singleton snippet
@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