标准写法:
// 在swift中,类方法是不允许定义静态变量的
static var once_t: dispatch_once_t = 0
static var instance: NetWorkTools?
class func sharedNetWorkTools() -> NetWorkTools {
dispatch_once(&once_t) {
instance = NetWorkTools()
}
return instance!
}
简单写法:
// swift中的let是线程安全的
// 用到时才会创建
static let instance: NetWorkTools = NetWorkTools()
class func sharedNetWorkTools() -> NetWorkTools {
return instance
}
##注意单例用到的时候才会创建!!!