Skip to content

Instantly share code, notes, and snippets.

@ziggear
Created May 26, 2013 08:41
Show Gist options
  • Save ziggear/5652106 to your computer and use it in GitHub Desktop.
Save ziggear/5652106 to your computer and use it in GitHub Desktop.
// Created by ziggear on 13-5-26
// fw & courtesy: http://cocoasamurai.blogspot.com/2011/04/singletons-your-doing-them-wrong.html
// usage:
// @implementation SomeClass
// ....
// kSingletonSynthesize(SomeClass)
// @end
//Thread-safe block
#define kRunInSecurutyBlock(lines) static dispatch_once_t pred; dispatch_once(&pred, ^{ \
lines \
});
//Singleton
#define kSingletonSynthesize(Class) + (id)sharedMan { \
static id sharedManager = nil;\
if (self == [Class class]) { \
kRunInSecurutyBlock(sharedManager = [[Class alloc] init];) \
} \
return sharedManager; \
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment