Recently in the development of a small application, encountered some objective-c above commonly used in the singleton mode, but Swift above still have a certain difference, repeatedly pour to find not according to Common sense (normal OC to Swift) card, so search some posts. Maybe it's Xcode or the SDK problem (I'm sure they won't show the untested code, right?). , some of the code in the post made a noticeable error, and the compilation failed. So with this article, share to everyone.
The original author implements a singleton, but the red code causes non-thread safety :
1 classvar Sharedinstance:tpscopemanager {2 Get {3 structStatic {4 Staticvar instance:tpscopemanager? =Nil5 }6 7 if! static.instance {8 static.instance = Tpscopemanager () 9}Ten One returnstatic.instance! A } -}
So there are the following solutions (personally feel Wonderful):
Global constants
First: Declare global variables directly
Let _singletonsharedinstance = Singleton ()class Singleton { ... }
Pros: The code is the most concise.
Cons: Code openness is more confusing
The second one makes up for the above shortcomings.
private Let _singletonsharedinstance = Singleton ()class Singleton { class var Sharedinstance:singleton { return _singletonsharedinstance }}
Note: Because type constants (that is, static constants of classes) are not supported, global constants are used here
This approach supports delay (lasy) initialization because swift delays initializing global constants (and variables), and the Let keyword is thread-safe. (implied: Global variables are also lazy-initialized, but not thread-safe?) I am not sure, please enlighten the great god )
Nested struct (estimate and translate to internal struct bar)
class Singleton { class var sharedinstance:singleton { struct Static { static Let Instance:singleton = Singleton () } return Static.instance }}
Class does not support type constants (that is, static constants for classes), but struct supports. With this, a similar effect can be achieved.
The original recommendation is to use an internal struct unless the type variable is supported in the new version
Dispatch_once (this really can't be translated)
The traditional OC approach is also supported in Swift, which obviously has no advantage over the other way, but write it down.
class Singleton { class var sharedinstance:singleton { struct Static { Static 0 static var Instance:singleton? = Nil } dispatch_once (&static.oncetoken) { = Singleton () } return static.instance! }}
(The same principle, the use of struct support type variable this advantage, to the OC of the dispatch_once mode of translation over)
As noted above, Apple has made it clear that deferred initialization is thread-safe, so there is no need to add another layer of dispatch_once or similar protections.
The lazy loading nature of global variables (the same as static members within structs and enums) is dispatch_once, so if you want to use Dispatch_once, you might as well declare a private global variable that guarantees thread safety and does not cause the code to be too open
I prefer the global variable approach (which, of course, is optimized), rather than nesting a layer of structs. What kind of people do you like?
Good English can directly see the original Http://stackoverflow.com/questions/24024549/dispatch-once-singleton-model-in-swift
Single-instance design pattern in ios--swift development (selected passage, non-original)