Skip to content

Instantly share code, notes, and snippets.

@craigrbruce
Last active December 14, 2015 09:08
Show Gist options
  • Save craigrbruce/5062226 to your computer and use it in GitHub Desktop.
Save craigrbruce/5062226 to your computer and use it in GitHub Desktop.
thread-safe objective c singleton syntax
//
// Created by craigrbruce on 1/03/13.
//
// To change the template use AppCode | Preferences | File Templates.
//
#import "Singleton.h"
@implementation Singleton {
}
-(id)init
{
NSAssert(NO, @"Cannot instantiate a singleton");
return nil;
}
-(id)initSingleton{
self = [super init];
if (self){
//do stuff here.
}
return self;
}
- (void)doStuff {
}
+(Singleton *) sharedSingleton{
static dispatch_once_t pred;
static Singleton* instance = nil;
dispatch_once(&pred, ^{instance = [[self alloc]initSingleton];});
return instance;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment