Arc and MRC compatible single-case mode

Source: Internet
Author: User

One, under the arc of the single example implementation

Description: The user-instantiated method controls a single execution, while opening a single-instance initialization method.

-(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;}

Second, the single-instance implementation under MRC

Description: In the user instantiation of the method to control a single execution, while opening a singleton initialization method, due to the current MRC so need to control the memory management method single execution, so compared to arc need to increase:

 
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;} 
Third, compatible with MRC and ARC macro definition

Note: In order to facilitate late reference, you can extract the singleton as a macro definition, given that the current manual count and automatic count are taken into account when extracting the conditional compilation:

#if!__has_feature (OBJC_ARC) ====== is currently arc#else====== MRC#ENDIF code: #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 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;} + (ID) copywithzone: (struct _nszone *) zone{return instance;} #endif

Iv. file references

1 references in. h Files Singleton_h (audio);

2 references in. m Files Singleton_m (audio);

Arc and MRC compatible single-case mode

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.