Skip to content

Instantly share code, notes, and snippets.

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
@implementation 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
@NSResponder

This comment has been minimized.

Copy link

@NSResponder NSResponder commented Oct 12, 2015

You mistyped "@implementation".

@virasio

This comment has been minimized.

Copy link
Owner Author

@virasio virasio commented Feb 2, 2017

You mistyped "@implementation".

@NSResponder, thanks, I fixed it.

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.