swift单例模式的几种方式:
private let _singleton = Singleton() class Singleton: NSObject { class var sharedInstance: Singleton { get { return _singleton } } }
class Singleton { class var sharedInstance: Singleton { get { struct SingletonStruct { static let singleton: Singleton = Singleton() } return SingletonStruct.singleton } } }
class Singleton { class var sharedInstance: Singleton { get { struct SingletonStruct { static var onceToken:dispatch_once_t = 0 static var singleton: Singleton? = nil } dispatch_once(&SingletonStruct.onceToken, { () -> Void in SingletonStruct.singleton = Singleton() }) return SingletonStruct.singleton! } } }
原文地址:http://blog.csdn.net/yanglishuan/article/details/40535407