方法一:
import Foundation
class SingleOne {
//单例
static let shareSingleOne = SingleOne()
}
方法二:
import Foundation
class SingleTwo {
//单例
class func shareSingleTwo()->SingleTwo{
struct Singleton{
static var onceToken : dispatch_once_t = 0
static var single:SingleTwo?
}
dispatch_once(&Singleton.onceToken,{
Singleton.single=shareSingleTwo()
}
)
return Singleton.single!
}
}
方法三:
import Foundation
//全局的常量
let single = SingleThree()
class SingleThree {
class var sharedInstance : SingleThree {
return single
}
}
方法四:
import Foundation class SingleFour { static var sharedInstance : SingleFour { struct Static { static let instance : SingleFour = SingleFour() } return Static.instance } }
原文链接:https://www.f2er.com/swift/325743.html