I had this implementation with Swift 2.0 and the Xcode suggestion is not only baffling but causes compilation error as well. it's a library where users are passing callfunc closures.
Before
protocol MyProtocol {
}
class Main
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!
}
After
private static var __once: () = {
MyProtocol.singleton = callfunc()
}()
open class func getSingleton(_ callfunc: () -> MyProtocol) -> MyProtocol {
singleton = MyProtocol.__once()
return singleton!
}
I basically need to pass parameter to __once function.
USER:
class Test: MyProtocol {
}
Main.getSingleton({Test()});
It's not a duplicate of Using a dispatch_once singleton model in Swift, a closure is being passed, it's a .framework and closure is passed in by the user of the library.