Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dependency injection example
class WelcomeView {
private let userDefaults: UserDefaults
init(userDefaults: UserDefaults) {
self.userDefaults = userDefaults;
}
func viewDidLoad() {
/// Show onboarding the first time User sees this screen;
if (userDefaults.bool(forKey: "appHasBeenAlreadyLaunched") == false) {
print("Welcome to the app! Let me show you the onboarding")
userDefaults.set(true, forKey: "appHasBeenAlreadyLaunched");
}
}
}
struct UserDefaults {
/// Read some keyed boolean value (on a persistent disk cache, or on a database, on a remote backend, etc.)
func bool(forKey: String) -> Bool
/// Write some boolean value (on a persistent disk cache, or on a database, on a remote backend, etc.)
func set(_ value: Bool, forKey: String) -> Bool
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment