Skip to content

Instantly share code, notes, and snippets.

@jarek-foksa
Created March 8, 2012 02:13
Show Gist options
  • Save jarek-foksa/1998092 to your computer and use it in GitHub Desktop.
Save jarek-foksa/1998092 to your computer and use it in GitHub Desktop.
@implementation MySingleton
static MySingleton* _instance = nil;
+(id)alloc {
@synchronized([MySingleton class]) {
NSAssert(_instance == nil, @"Error. Don't use alloc on singloetons.");
_instance = [super alloc];
return _instance;
}
return nil;
}
+(MySingleton*)init {
@synchronized([MySingleton class]) {
if (!_instance) {
[[self alloc] _init];
}
return _instance;
}
return nil;
}
-(id)_init {
NSLog(@"initing singleton");
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment