Ways to create a singleton
1 Import afnetworking 2 3 class Networktools:afhttpsessionmanager {4 5 // Let is thread-safe 6 // use a singleton instance just to get networktools This class takes out this constant attribute to get a single case 7 static Let Shareinstance:networktools = networktools ()89 }
How to use the concrete
1 Import UIKit2 3 classViewcontroller:uiviewcontroller {4 5 Overridefunc viewdidload () {6 super.viewdidload ()7 //The following two singleton addresses are consistent after the compilation is run,8 print (networktools.shareinstance)9 print (networktools.shareinstance)Ten } One A OverrideFunc Touchesbegan (_ Touches:set<uitouch>, withEvent: Uievent?) { - //after running compilation, the following two singleton addresses are inconsistent, using a singleton must take a constant attribute, and using networking directly creates an instance - print (networktools.shareinstance) the print (Networktools ()) - } -}
There are two main types of single cases:
1. It's always an example to get through shareinstance.
2. Whatever it is, getting it is always an example.
Single Example: Example with afnetworking encapsulation