1.最经典的
class TheOneAndOnlyKraken {
static let sharedInstance = TheOneAndOnlyKraken()
private init() {}
//This prevents others from using the default '()' initializer for this class.
}
2.只执行一次法
class TheOneAndOnlyKraken {
class
var
sharedInstance: TheOneAndOnlyKraken {
struct Static {
static
var
onceToken: dispatch_once_t = 0
static
var
instance: TheOneAndOnlyKraken? = nil
}
dispatch_once(&Static.onceToken) {
Static.instance = TheOneAndOnlyKraken()
}
return
Static.instance!
}
}
3.结构体法
class TheOneAndOnlyKraken {
class
var
sharedInstance: TheOneAndOnlyKraken {
struct Static {
static let instance = TheOneAndOnlyKraken()
}
return
Static.instance
}
}
4.全局变量法
private let sharedKraken = TheOneAndOnlyKraken()
class TheOneAndOnlyKraken {
class
var
sharedInstance: TheOneAndOnlyKraken {
return
sharedKraken
}
}