Three swift ways to implement a single case pattern: global variables, internal variables, dispatch_once
1. Global variables
Private Let _singleton = Singleton ()
class Singleton:nsobject {
class var Sharedinstance:singleton {get
{
return _singleton
}}}
2. Internal variables
Class Singleton {
class var Sharedinstance:singleton {get
{
struct) singletonstruct {
static let sin Gleton:singleton = Singleton ()
} return
Singletonstruct.singleton
}
}
3. Dispatch_once mode
Class Singleton {
class var Sharedinstance:singleton {get
{
struct singletonstruct {
static var Oncetok en:dispatch_once_t = 0
static var Singleton:singleton = nil
}
dispatch_once (& Singletonstruct.oncetoken, {()-> Void in
Singletonstruct.singleton = Singleton ()
})
return singletonstruct.singleton!}}}
The above mentioned is the entire content of this article, I hope you can enjoy.