Skip to content

Instantly share code, notes, and snippets.

@Galeas
Last active April 22, 2020 21:57
Show Gist options
  • Save Galeas/7431739 to your computer and use it in GitHub Desktop.
Save Galeas/7431739 to your computer and use it in GitHub Desktop.
Singleton in Interface Builder.
+ (instancetype)handler
{
__strong static id *_sharedInstance = nil;
static dispatch_once_t onlyOnce;
dispatch_once(&onlyOnce, ^{
_sharedInstance = [[self _alloc] _init];
});
return _sharedInstance;
}
+ (id) allocWithZone:(NSZone*)z
{
return [self handler];
}
+ (id) alloc
{
return [self handler];
}
- (id) init
{
return self;
}
+ (id)_alloc
{
return [super allocWithZone:NULL];
}
- (id)_init
{
self = [super init];
if (self) {
// Common setup
}
return self;
}
@chbeer
Copy link

chbeer commented Apr 22, 2020

__strong static id *_sharedInstance = nil; should be id _sharedInstance without a *.

@Galeas
Copy link
Author

Galeas commented Apr 22, 2020

@chbeer thank you, I missed that typo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment