Skip to content

Instantly share code, notes, and snippets.

@EugeneRash
Created December 16, 2013 12:39
Show Gist options
  • Save EugeneRash/7986363 to your computer and use it in GitHub Desktop.
Save EugeneRash/7986363 to your computer and use it in GitHub Desktop.
Modern Obj-C Singleton (c)http://habrahabr.ru/users/code_monkey/
@interface MySingleton : NSObject
+(instancetype) sharedInstance;
+(instancetype) alloc __attribute__((unavailable("alloc not available, call sharedInstance instead")));
-(instancetype) init __attribute__((unavailable("init not available, call sharedInstance instead")));
+(instancetype) new __attribute__((unavailable("new not available, call sharedInstance instead")));
@end
@implementation MySingleton
+(instancetype) sharedInstance {
static dispatch_once_t pred;
static id shared = nil;
dispatch_once(&pred, ^{
shared = [[super alloc] initUniqueInstance];
});
return shared;
}
-(instancetype) initUniqueInstance {
return [super init];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment