Single-instance mode compatible with ARC and MRC, and compatible with arcmrc
I. single-instance implementation under ARC |
Note: The method of user instantiation controls a single execution, while opening the initialization method of the Singleton.
-(instancetype)init{ self=[super init]; if(self){ static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ }); } return self; }static id instance; +(instancetype)allocWithZone:(struct _NSZone *)zone{ static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ instance=[super allocWithZone:zone]; }); return instance;} + (instancetype) shareAudio{ static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ instance=[[self alloc]init]; }); return instance; } +(id)copyWithZone:(struct _NSZone *)zone{ return instance;}
Ii. single-instance implementation in MRC |
Note: The method of user instantiation controls a single execution, and the initialization method of the Singleton is enabled. Because MRC is currently used, the method of controlling the parameter memory management needs to be executed at a time. Therefore, this method needs to be added compared with ARC:
static id instance; +(instancetype)allocWithZone:(struct _NSZone *)zone{ static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ instance=[super allocWithZone:zone]; }); return instance; } + (instancetype) shareAudio{ static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ instance=[[self alloc]init]; }); return instance; } -(oneway void)release{ } -(instancetype)autorelease{ return instance; } -(instancetype)retain{ return instance; } -(NSUInteger)retainCount{ return 1; }
Iii. Compatibility with macro definitions of MRC and ARC |
Note: To facilitate later reference, you can extract a singleton as a macro definition. In view of the current manual count and automatic count, Conditional compilation is introduced:
# If! _ Has_feature (objc_arc) ===== current is ARC # else ===== current is MRC # endifCode:# Define singleton_h (name) + (instancetype) share ## name; # if! _ Has_feature (objc_arc) # define singleton_m (name) \ static id instance; \ + (instancetype) allocWithZone :( struct _ NSZone *) zone {\ static dispatch_once_t onceToken; \ dispatch_once (& onceToken, ^ {\ instance = [super allocWithZone: zone] ;\}); \ return instance ;\}\\ + (instancetype) share ## name {\ static dispatch_once_t onceToken; \ dispatch_once (& onceToken, ^ {\ instance = [[self alloc] init] ;\}); \ return instance; \}\-(oneway void) release {\}\\-(instancetype) autorelease {\ return instance ;\}\\-(instancetype) retain {\ return instance; \}\+ (id) copyWithZone :( struct _ NSZone *) zone {\ return instance; \}\-(NSUInteger) retainCount {\ return 1; \}# else # define singleton_m (name) \ static id instance; \ + (instancetype) allocWithZone :( struct _ NSZone *) zone {\ static deleoncetoken; \ dispatch_once (& onceToken, ^ {\ instance = [super allocWithZone: zone] ;\}); \ return instance ;\}\\+ (instancetype) share ## name {\ static dispatch_once_t onceToken; \ dispatch_once (& onceToken, ^ {\ instance = [[self alloc] init] ;\}); \ return instance ;\}\+ (id) copyWithZone :( struct _ NSZone *) zone {\ return instance ;\}# endif
1. Reference singleton_h (audio) in the. h file );
2. Reference singleton_m (audio) in the. m file );