//. h file#defineHmsingletonh (name) + (instancetype) shared# #name;//. m file#if__has_feature (OBJC_ARC)#defineHMSINGLETONM (name)StaticID _instace; + (ID) Allocwithzone: (struct_nszone *) Zone {Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{_instace=[Super Allocwithzone:zone]; }); return_instace; } +(instancetype) shared# #name {Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{_instace=[[Self alloc] init]; }); return_instace; } -(ID) Copywithzone: (Nszone *) Zone {return_instace; }#else #defineHMSINGLETONM (name)StaticID _instace; + (ID) Allocwithzone: (struct_nszone *) Zone {Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{_instace=[Super Allocwithzone:zone]; }); return_instace; } +(instancetype) shared# #name {Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{_instace=[[Self alloc] init]; }); return_instace; } -(ID) Copywithzone: (Nszone *) Zone {return_instace; } -(OneWayvoid) Release {}-(ID) retain {returnSelf ;} -(Nsuinteger) Retaincount {return 1;} -(ID) autorelease {returnSelf ;}#endif
System comes with judgment condition statement
#if _has_feature (BBJC_ARC)
If it's Arc mode, implement the code inside
#else
If it is the MVC pattern, implement the code inside
#endif
Import arc and MVC singleton patterns into the same header file