Skip to content

Instantly share code, notes, and snippets.

@tmr111116
Forked from lukeredpath/ExampleClass.m
Last active December 14, 2015 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmr111116/5075912 to your computer and use it in GitHub Desktop.
Save tmr111116/5075912 to your computer and use it in GitHub Desktop.
@implementation MySharedThing
+ (id)sharedInstance
{
DEFINE_SHARED_INSTANCE_USING_BLOCK(^{
return [[self alloc] init];
});
}
@end
#define DEFINE_SHARED_INSTANCE_USING_BLOCK(block) \
static dispatch_once_t pred = 0; \
__strong static id _sharedObject = nil; \
dispatch_once(&pred, ^{ \
_sharedObject = block(); \
}); \
return _sharedObject;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment