Skip to content

Instantly share code, notes, and snippets.

@joelrfcosta
Forked from lukeredpath/ExampleClass.m
Last active June 5, 2021 18:26
Show Gist options
  • Save joelrfcosta/4032287 to your computer and use it in GitHub Desktop.
Save joelrfcosta/4032287 to your computer and use it in GitHub Desktop.
Singleton macro with blocks using GCD
#define SingletonWithBlock(block) static dispatch_once_t pred = 0; \
__strong static id _sharedObject = nil; \
dispatch_once(&pred, ^{ \
_sharedObject = block(); \
}); \
return _sharedObject; \
@implementation SingletonClass
+ (id)sharedInstance
{
SingletonWithBlock(^{
return [[self alloc] init];
});
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment