Swift中创建单例的方法

前端之家收集整理的这篇文章主要介绍了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

猜你在找的Swift相关文章