Last active
December 14, 2015 09:08
-
-
Save craigrbruce/5062226 to your computer and use it in GitHub Desktop.
thread-safe objective c singleton syntax
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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