| 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
1 references in. h Files Singleton_h (audio);
2 references in. m Files Singleton_m (audio);
Arc and MRC compatible single-case mode