Skip to content

Instantly share code, notes, and snippets.

@jamesnesfield
Created February 7, 2014 14:39
Show Gist options
  • Save jamesnesfield/8863750 to your computer and use it in GitHub Desktop.
Save jamesnesfield/8863750 to your computer and use it in GitHub Desktop.
singleton macro iOS
//from Urban Airship iOS SDK "1.1.2"
#define SINGLETON_INTERFACE(CLASSNAME) \
+ (CLASSNAME*)shared; \
#define SINGLETON_IMPLEMENTATION(CLASSNAME) \
\
static CLASSNAME* g_shared##CLASSNAME = nil; \
\
+ (CLASSNAME*)shared \
{ \
static dispatch_once_t sharedOncePredicate##CLASSNAME; \
\
dispatch_once(&sharedOncePredicate##CLASSNAME, ^{ \
g_shared##CLASSNAME = [[self alloc] init]; \
}); \
return g_shared##CLASSNAME; \
} \
\
+ (id)allocWithZone:(NSZone*)zone \
{ \
static dispatch_once_t allocOncePredicate##CLASSNAME; \
dispatch_once(&allocOncePredicate##CLASSNAME, ^{ \
if (g_shared##CLASSNAME == nil) { \
g_shared##CLASSNAME = [super allocWithZone:zone]; \
} \
}); \
return g_shared##CLASSNAME; \
} \
\
- (id)copyWithZone:(NSZone*)zone \
{ \
return self; \
} \
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment