前端之家收集整理的这篇文章主要介绍了
Swift中创建单例的方法,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_
404_0@class TodoList {
class var sharedInstance : TodoList {
struct Static {
static let instance : TodoList = TodoList()
}
return Static.instance
}
}这是Swift1.2之前单例的实现方式,Swift1.2中
添加了对static let和static var这样存储类变量的
支持,当前Swift单例设置的最佳实践之一是:
@H_
404_0@class MyManager {
private static let sharedInstance = MyManager()
class var sharedManager : MyManager {
return sharedInstance
}
}
原文链接:https://www.f2er.com/swift/325909.html