Skip to content

Instantly share code, notes, and snippets.

@intari

intari/Singleton.h

Forked from virasio/Singleton.h
Created Feb 16, 2016
Embed
What would you like to do?
Singleton (Objective-C with ARC)
@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
@implimentation MySingleton
// ...
+ (instancetype) sharedInstance {
static id sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[super alloc] initInstance];
});
return sharedInstance;
}
- (instancetype) initInstance {
self = [super init];
// Do any other initialisation stuff here
// ...
return self;
}
// ...
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.