The difference between swift and OC two grammar writing single cases
For example, write a single example of a networktools
(1) OC Write a single case
1+(instancetype) sharednetworktools {2 Static IDinstance;3 4 Staticdispatch_once_t Oncetoken;5 6Dispatch_once (&oncetoken, ^{7Instance =[[Self alloc] init];8 //Here you can do some initialization9 });Ten One returninstance; A}
(2) Swift writes a single example
1 //to define a private static member2 //' Let ' is thread-safe3 //This code is lazy to load, and will not run until the first call.4 Private StaticLet instance =Networktools ()5 6 classFunc Sharednetworktools ()Networktools {7 returninstance8}
If you want to initialize some properties beforehand, you can write this
1 Private StaticLet Instance:networktools = {2Let Networktool =Networktools ()3 //This initializes the properties4 5 returnNetworktool6 }()7 8 classFunc Sharednetworktools ()Networktools {9 returninstanceTen}
The difference between "Swfit" swift and OC two grammar writing single cases