Skip to content

Instantly share code, notes, and snippets.

@dominik-hadl
Forked from lukeredpath/ExampleClass.m
Last active December 16, 2015 23:59
Show Gist options
  • Save dominik-hadl/5517734 to your computer and use it in GitHub Desktop.
Save dominik-hadl/5517734 to your computer and use it in GitHub Desktop.
GCDSingleton Macro + @ArvinB changes.
/*!
* @function Singleton GCD Macro
*/
#ifndef SINGLETON_GCD
#define SINGLETON_GCD(classname) \
\
+ (classname *)shared##classname { \
\
static dispatch_once_t pred; \
__strong static classname * shared##classname = nil;\
dispatch_once( &pred, ^{ \
shared##classname = [[self alloc] init]; }); \
return shared##classname; \
}
#endif
@interface MyClass : NSObject
+ (MyClass *) sharedMyClass;
@end
#import "MyClass.h"
@implementation MyClass
SINGLETON_GCD(MyClass);
- (id) init {
if ( (self = [super init]) ) {
// Initialization code here.
}
return self;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment