方式一: (类似OC)
class SingletonDispatch{
class var shareInstance : SingletonDispatch {
//结构体
struct Static {
static var onceToken : dispatch_once_t = 0
static var instance : SingletonDispatch? = nil
}
//创建
dispatch_once(&Static.onceTokec){
Static.instance = SingletonDispatch()
}
return Static.instance!
}
}
方式二:
class SingletonDispatch {
class shareInstance : SingletonDispatch {
struct Static {
static var instance = SingletonDispatch()
}
return Static.instance
}
}
方式三:
class SingletonDispatch {
static let share Instance = SingletonDispatch()
private init() {}
}
//单例的创建
let single = SingletonDispatch.shareInstance
时间: 2024-08-05 14:23:12