Instantly share code, notes, and snippets.

Embed
What would you like to do?
iOS Objective-C Singleton Pattern with Macro!!!
#define MACRO_SINGLETON_PATTERN_H \
+ (instancetype)sharedInstance;\
- (instancetype)init NS_UNAVAILABLE;
#define MACRO_SINGLETON_PATTERN_M(MACRO_SINGLETON_PATTERN_INTT)\
static id _instance;\
+ (instancetype)allocWithZone:(struct _NSZone *)zone\
{\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
_instance = [super allocWithZone:zone];\
});\
return _instance;\
}\
\
+ (instancetype)sharedInstance\
{\
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [[self alloc] init]; \
}); \
return _instance; \
}\
- (id)copyWithZone:(NSZone *)zone\
{\
return _instance;\
}\
\
- (id)mutableCopyWithZone:(NSZone *)zone\
{\
return _instance;\
}\
- (instancetype)init\
{\
self = [super init];\
if (self) {\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
MACRO_SINGLETON_PATTERN_INTT\
});\
}\
return self;\
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment