Last active
March 30, 2017 17:14
-
-
Save sundeepgupta/44df0528183fd88969bd4c8b22a04f86 to your computer and use it in GitHub Desktop.
Swift singleton with initialization injection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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