Skip to content

Instantly share code, notes, and snippets.

@0xc010d
Created November 27, 2012 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0xc010d/4157064 to your computer and use it in GitHub Desktop.
Save 0xc010d/4157064 to your computer and use it in GitHub Desktop.
IB compliant Singleton in ARC
@interface Singleton : NSObject
+ (instancetype)sharedInstance;
@end
@implementation Singleton
+ (instancetype)sharedInstance {
__strong static id _sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedInstance = [[self _alloc] _init];
});
return _sharedInstance;
}
+ (instancetype)allocWithZone:(NSZone *)zone {
return [self sharedInstance];
}
+ (instancetype)alloc {
return [self sharedInstance];
}
- (instancetype)init {
return self;
}
+ (instancetype)_alloc {
return [super allocWithZone:NULL];
}
- (instancetype)_init {
return [super init];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment