我有Swift 2.0的这个实现,Xcode建议不仅令人困惑,而且还会导致编译错误.它是一个用户传递callfunc闭包的库.
之前
protocol MyProtocol {
}
Run Code Online (Sandbox Code Playgroud)
主要课程
private static var t: dispatch_once_t = 0
private static var singleton: MyProtocol?
public class func getSingleton(callfunc: () -> MyProtocol) -> MyProtocol {
dispatch_once(&self.t) {
self.singleton = callfunc()
}
return singleton!
}
Run Code Online (Sandbox Code Playgroud)
后
private static var __once: () = {
MyProtocol.singleton = callfunc()
}()
open class func getSingleton(_ callfunc: () -> MyProtocol) -> MyProtocol {
singleton = MyProtocol.__once()
return singleton!
}
Run Code Online (Sandbox Code Playgroud)
我基本上需要将参数传递给__once函数.
用户:
class Test: MyProtocol {
}
Main.getSingleton({Test()});
Run Code Online (Sandbox Code Playgroud)
它不是在Swift中使用dispatch_once单例模型,传递闭包,它是一个.framework,闭包是由库的用户传递的.
Pat*_*her 17
我通常喜欢这种模式:
final class MyClass {
static let shared = MyClass()
}
然后你可以调用MyClass.shared来获得你的单身人士.
| 归档时间: |
|
| 查看次数: |
18238 次 |
| 最近记录: |