Single-instance mode compatible with ARC and MRC, and compatible with arcmrc

Source: Internet
Author: User

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
Iv. File Reference

1. Reference singleton_h (audio) in the. h file );

2. Reference singleton_m (audio) in the. m file );

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.