Today ran to the university to see the source code, just start school has a lot of students on self-study, a long time did not come to college, or the university quiet.
Today saw a bit of Alamofire source code, where the manager of the single example is as follows:
public static Let Sharedinstance:manager = {let configuration = Nsurlsessionconfiguration.defaultsessionconfiguration () configuration. Httpadditionalheaders = Manager.defaulthttpheaders return Manager (configuration:configuration) } ()
The above code uses the public static let to define a singleton variable sharedinstance. The first thing I think of when I look at this single case is to write this and then set the Init method to private so that I can implement the singleton. But looked at the source code, the source of the Init method is public, and later explained to himself, may be the author left the user to create their own instance of the entrance. But think of it as a bit of a violation of the idea of a single case.
In addition, creating a singleton in OC is generally as follows:
+ (Accountmanager *) Sharedmanager { static accountmanager *sharedaccountmanagerinstance = nil; static dispatch_once_t predicate; Dispatch_once (&predicate, ^{ sharedaccountmanagerinstance = [[Self alloc] init]; }); return sharedaccountmanagerinstance; }
On the web there is also the decision whether sharedaccountmanagerinstance is empty and then created, but this way creates a singleton, in the case of multithreading, prone to problems. Specific problems can be Baidu.
Here are the modifiers for type methods and properties: the Static and Class keywords. First list to an article, after reading this article and then come back to see my writing behind. This article is written by Wang Wei, the quality is very high, address: http://swifter.tips/static-class/.
The use of static and class is described in detail in Wang Wei's article. A simple generalization is the use of static, computed properties, and methods using class in a struct, enum, which stores properties using Static,class. I did my own tests. You can also use static to calculate properties and methods in class, so if you don't remember, you can use static directly.
Turn around and take a look at the declaration of the shared instance in the Alamofire code above, and see for the first time that I thought it was a computed property because it followed a lot of code. But a closer look, in fact, is a storage property, after the equals sign is actually a trailing closure, and then using parentheses, immediately get the value of the closure, and then assign the value to the storage property. (For this may be wrong, I have seen this situation is called what, may be mistaken, if the wrong trouble to me to point out).
Here's the ad:
I shared the playground of Swift 2.0, which was collated on GitHub. Address Https://github.com/mengxiangyue/The-Swift-2.0-Programming-Language-playground
Corresponds to the latest release of the SWIFT programming Language Swift 2.0 release. These playground Basic is a book in the knowledge Point of a summary, if do not want to read English book People see these palyground are finished, also basically read the contents of the book.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Thinking about Alamofire Manager single case