Singleton: Only one instance of the entire program, no matter how many times it is created, refers to the same memory address
Here is the implementation of the method, as long as the copy can be used directly (create a. h header file, copy it, call the words in the. h file using Wyssingletonh (Write your class name here), and then call Wyssingletonm in the. m file (write your class name here), This includes the ARC and MRC implementations):
//. h file#defineWyssingletonh (name) + (instancetype) shared# #name;//. m file#if__has_feature (OBJC_ARC)#defineWYSSINGLETONM (name)Static ID_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 #defineWYSSINGLETONM (name)Static ID_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
iOS Singleton mode