One-sentence OC Singleton mode and one-sentence OC Mode
Create a new header file and define the following macro:
//. H file implementation # define SingletonH (methodName) + (instancetype) shared # methodName ;//. implementation of the m file # if _ has_feature (objc_arc) // It is ARC # define SingletonM (methodName) \ static id _ instace = nil; \ + (id) allocWithZone :( struct _ NSZone *) zone \ {\ if (_ instace = nil) {\ static dispatch_once_t onceToken; \ dispatch_once (& onceToken, ^ {\ _ instace = [super allocWithZone: zone] ;\}) ;\}\ return _ instace ;\}\\-(id) init \{\ static dispatch_once_t onceToken; \ dispatch_once (& onceToken, ^ {\ _ instace = [super init] ;\}); \ return _ instace ;\}\+ (instancetype) shared # methodName \ {\ return [[self alloc] init] ;\}\+ (id) copyWithZone :( struct _ NSZone *) zone \{\ return _ instace; \}\+ (id) mutableCopyWithZone :( struct _ NSZone *) zone \ {\ return _ instace ;\}# else // not ARC # define SingletonM (methodName) \ static id _ instace = nil; \ + (id) allocWithZone :( struct _ NSZone *) zone \ {\ if (_ instace = nil) {\ static dispatch_once_t onceToken; \ dispatch_once (& onceToken, ^ {\ _ instace = [super allocWithZone: zone] ;\}) ;\}\ return _ instace ;\}\-(id) init \ {\ static dispatch_once_t onceToken; \ dispatch_once (& onceToken, ^{\_ instace = [super init] ;\}); \ return _ instace; \}\+ (instancetype) shared # methodName \ {\ return [[self alloc] init] ;\}\- (oneway void) release \ {\}\\-(id) retain \ {\ return self ;\}\\-(NSUInteger) retainCount \ {\ return 1; \} \ + (id) copyWithZone :( struct _ NSZone *) zone \ {\ return _ instace ;\}\+ (id) mutableCopyWithZone :( struct _ NSZone *) zone \ {\ return _ instace ;\}# endif
Then
. H file write SingletonH (MyMethodName)
. M file write SingletonM (MyMethodName)
Done!