Skip to content

Instantly share code, notes, and snippets.

@PhilippeCuvillier
Last active October 31, 2018 19:05
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 PhilippeCuvillier/65567438a3b1582707fec53e33534245 to your computer and use it in GitHub Desktop.
Save PhilippeCuvillier/65567438a3b1582707fec53e33534245 to your computer and use it in GitHub Desktop.
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