1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| #define SINGLETON_INTERFACE(className) + (instancetype)shared##className;
#define SINGLETON_IMPLEMENTATION(className) \ static id instance; \ + (instancetype)allocWithZone:(struct _NSZone *)zone { \ static dispatch_once_t onceToken; \ dispatch_once(&onceToken, ^{ \ instance = [super allocWithZone:zone]; \ }); \ return instance; \ } \ \ + (instancetype)shared##className { \ static dispatch_once_t onceToken; \ dispatch_once(&onceToken, ^{ \ instance = [[self alloc] init]; \ }); \ return instance; \ } \ \ - (id)copyWithZone:(NSZone *)zone { \ return instance; \ }
|