Swift中创建单例的方法

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单例设置的最佳实践之一是:
class MyManager  {
    private static let sharedInstance = MyManager()
    class var sharedManager : MyManager {
        return sharedInstance
    }
}

你可能感兴趣的:(单例)