我正在努力制定一个合适的单例模型用于Swift的使用。到目前为止,我已经能够得到一个非线程安全的模型工作为:

class var sharedInstance: TPScopeManager {
    get {
        struct Static {
            static var instance: TPScopeManager? = nil
        }

        if !Static.instance {
            Static.instance = TPScopeManager()
        }

        return Static.instance!
    }
}

将单例实例包装在Static结构中应该允许单个实例不与单例实例发生冲突,而不需要复杂的命名方案,并且它应该使事情相当私密。但是,这个模型显然不是线程安全的。所以我尝试将dispatch_once添加到整个事情中:

class var sharedInstance: TPScopeManager {
    get {
        struct Static {
            static var instance: TPScopeManager? = nil
            static var token: dispatch_once_t = 0
        }

        dispatch_once(Static.token) { Static.instance = TPScopeManager() }

        return Static.instance!
    }
}

但是我在dispatch_once行上得到了一个编译器错误:

不能将表达式的类型“Void”转换为类型“()”

我尝试了几种不同的语法变体,但它们似乎都有相同的结果:

dispatch_once(Static.token, { Static.instance = TPScopeManager() })

在Swift中dispatch_once的正确用法是什么?我最初认为问题出在错误消息中的()块上,但我看得越多,就越觉得可能是正确定义dispatch_once_t的问题。


当前回答

我要求我的单例允许继承,而这些解决方案实际上都不允许。所以我想到了这个:

public class Singleton {
    private static var sharedInstanceVar = Singleton()

    public class func sharedInstance() -> Singleton {
        return sharedInstanceVar
    }
}


public class SubSingleton: Singleton {

    private static var sharedInstanceToken: dispatch_once_t = 0

    public class override func sharedInstance() -> SubSingleton {
        dispatch_once(&sharedInstanceToken) {
            sharedInstanceVar = SubSingleton()
        }
    return sharedInstanceVar as! SubSingleton
    }
}

这样,当首先执行Singleton. sharedinstance()时,它将返回Singleton的实例 当首先执行SubSingleton. sharedinstance()时,它将返回创建的SubSingleton实例。 如果执行了上述操作,则SubSingleton.sharedInstance() is Singleton为true,并且使用相同的实例。

第一种脏方法的问题是,我不能保证子类会实现dispatch_once_t,并确保每个类只修改一次sharedInstanceVar。

我将尝试进一步完善它,但是看看是否有人对此有强烈的反对(除了它很冗长并且需要手动更新之外),这将是很有趣的。

其他回答

在看到David的实现之后,似乎没有必要使用单个类函数instanceMethod,因为let所做的事情与sharedInstance类方法几乎相同。你所要做的就是把它声明为一个全局常数,就是这样。

let gScopeManagerSharedInstance = ScopeManager()

class ScopeManager {
   // No need for a class method to return the shared instance. Use the gScopeManagerSharedInstance directly. 
}

简而言之,

class Manager {
    static let sharedInstance = Manager()
    private init() {}
}

您可能需要阅读文件和初始化

的静态成员的惰性初始化式 Structs和enum)在第一次访问global时运行,并且 作为dispatch_once启动,以确保初始化为 原子。

Swift过去实现单例,无非是三种方式:全局变量、内部变量和dispatch_once方式。

这里有两个不错的单品。(注:无论哪种写法都一定要注意init()私有化方法。因为在Swift中,所有对象的构造函数default都是public,需要重写init就可以变成private,防止该类的其他对象通过默认的初始化方法来创建对象。)

方法1:

class AppManager {
    private static let _sharedInstance = AppManager()

    class func getSharedInstance() -> AppManager {
       return _sharedInstance
    }

    private init() {} // Privatizing the init method
}

// How to use?
AppManager.getSharedInstance()

方法2:

class AppManager {
    static let sharedInstance = AppManager()

    private init() {} // Privatizing the init method
}

// How to use?
AppManager.sharedInstance

这是具有线程安全功能的最简单的一个。即使其他线程想要访问同一个单例对象,它们也不能访问。斯威夫特3/4

struct DataService {

    private static var _instance : DataService?

    private init() {}   //cannot initialise from outer class

    public static var instance : DataService {
        get {
            if _instance == nil {
                DispatchQueue.global().sync(flags: .barrier) {
                    if _instance == nil {
                        _instance = DataService()
                    }
                }
            }
            return _instance!
        }
    }
}

我建议使用枚举,就像您在Java中使用的那样,例如。

enum SharedTPScopeManager: TPScopeManager {
    case Singleton
}