The so-called Singleton is: an application (APP) has only one instantiation object, this object is a singleton, generally used for music player and Tool class
Here we teach you how to create a singleton manually, one is to use mutexes, and the other is to take advantage of Dispatch's one-time execution.
1//Create a singleton by mutual exclusion lock2+(instancetype) Sharednetworktools3 {4 //static modifier5 StaticNetworktools *tools =Nil;6 //To determine if an instantiated object exists7 //Mutual exclusion Lock8 @synchronized (self) {9 if(Tools = =Nil) {TenTools = [NetworktoolsNew]; One } A } - returntools; - }
the //One-time execution create a single case -+(instancetype) sharednetworktoolsonce - { - //static modifier + StaticNetworktools *tools =Nil; - + //thread-safe execution at once A Staticdispatch_once_t Oncetoken; atDispatch_once (&oncetoken, ^{ - if(!tools) { -Tools =[[Networktools alloc]init]; - } - }); - returntools; in -}
Manually create a single case