Skip to content

Instantly share code, notes, and snippets.

@sundeepgupta
Last active March 30, 2017 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sundeepgupta/44df0528183fd88969bd4c8b22a04f86 to your computer and use it in GitHub Desktop.
Save sundeepgupta/44df0528183fd88969bd4c8b22a04f86 to your computer and use it in GitHub Desktop.
Swift singleton with initialization injection
class CoreGlobal {
fileprivate static let shared = CoreGlobal()
fileprivate let device: DeviceManager
private class Config {
var deviceManager: DeviceManager?
}
private static let config = Config()
@discardableResult
public static func setup(with deviceManager: DeviceManager) -> CoreGlobal {
CoreGlobal.config.deviceManager = deviceManager
return CoreGlobal.shared
}
private init() {
guard let deviceManager = CoreGlobal.config.deviceManager else {
fatalError("Must call `setup(_:)` first.")
}
device = deviceManager
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment