Real implementation of Singleton mode (ACR&MRC)
In iOS development, the singleton mode is a very common mode, although we achieve less, but the system provides a lot of the arrival mode for us, such as the most common uiapplication,notification,
So this article is a brief introduction, we develop if you want to implement a singleton mode how to achieve!
The singleton pattern, as the name implies, is only one instance, which ensures that a class has only one instance, and instantiates it itself and provides it to the system as a whole. It is often used to do application-level shared resource control. This mode is used very frequently, and through a singleton class, it is possible to pass data between different windows.
To implement a singleton class in Objective-c (MRC), you need to do at least the following four steps:
- 1, for a singleton object to implement a static instance, and initialize, and then set to nil,
- 2. Implement an instance construction method to check if the static instance declared above is nil, and if it is new and returns an instance of this class,
- 3, rewrite the Allocwithzone method, to ensure that other people directly use Alloc and Init to try to obtain a new instance of the time does not produce a new instance,
- 4, the appropriate implementation of Allocwithezone,copywithzone,release and Autorelease
Example: Create a singleton function for Rootviewcontroller:
The code below can be copied directly to the header file
1 #defineSingleton_h (name) + (instancetype) shared# #name2#if__has_feature (OBJC_ARC)//ARC3 4 #defineSingleton_m (name)5 Static ID_instance;6+(ID) Allocwithzone: (struct_nszone *) Zone7 {8 Staticdispatch_once_t Oncetoken;9Dispatch_once (&oncetoken, ^{Ten_instance =[Super Allocwithzone:zone]; One }); A return_instance; - } - the+(instancetype) shared# #name - { - Staticdispatch_once_t Oncetoken; -Dispatch_once (&oncetoken, ^{ +_instance =[[Self alloc] init]; - }); + return_instance; A } at -+(ID) Copywithzone: (struct_nszone *) Zone - { - return_instance; - } - #else //Non-Arc in #defineSingleton_m (name) - Static ID_instance; to+(ID) Allocwithzone: (struct_nszone *) Zone + { - Staticdispatch_once_t Oncetoken; theDispatch_once (&oncetoken, ^{ *_instance =[Super Allocwithzone:zone]; $ });Panax Notoginseng return_instance; - } the ++(instancetype) shared# #name A { the Staticdispatch_once_t Oncetoken; +Dispatch_once (&oncetoken, ^{ -_instance =[[Self alloc] init]; $ }); $ return_instance; - } - the+(ID) Copywithzone: (struct_nszone *) Zone - {Wuyi return_instance; the } --(OneWayvoid) Release Wu { - About } $-(instancetype) autorelease - { - return_instance; - } A-(instancetype) retain + { the return_instance; - } $-(Nsuinteger) Retaincount the { the return 1; the}
But in the non-arc is the MRC implementation of the same way, we need to do the corresponding memory management.
MRC to rewrite four methods:
1-(OneWayvoid) Release2 3 {4 5 }6 7-(instancetype) autorelease8 9 {Ten One returnSelf ; A - } - the-(instancetype) retain{ - - returnSelf ; - + } - +-(Nsuinteger) retaincount{ A at return 1; - -}
iOS Development--practical implementation of OC & Singleton mode (ACR&MRC)