Create a ticket manually and manually
The so-called Singleton is: an application (app) has only one instantiated object, which is a singleton. It is generally used for music players and tools.
Here we will teach you how to manually create a singleton. One is to use mutex lock, and the other is to use dispatch for one-time execution.
1 // create a singleton 2 + (instancetype) sharedNetWorkTools 3 {4/static modify 5 static NetworkTools * tools = nil; 6 // determine whether the instantiated object exists 7 // mutex lock 8 @ synchronized (self) {9 if (tools = nil) {10 tools = [NetworkTools new]; 11} 12} 13 return tools; 14}
15 // create a singleton 16 + (instancetype) sharedNetWorkToolsOnce17 {18 // static modifier 19 static NetworkTools * tools = nil; 20 21 // One-time execution thread security 22 static dispatch_once_t onceToken; 23 dispatch_once (& onceToken, ^ {24 if (! Tools) {25 tools = [[NetworkTools alloc] init]; 26} 27}); 28 return tools; 29 30}