Last active
December 9, 2018 22:14
-
-
Save roman-wb/9bb468797e56f352e6f1117030591f2b to your computer and use it in GitHub Desktop.
Singleton class for store boolean Settings in UserDefaults iOS (Swift)
This file contains hidden or 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
| import UIKit | |
| class Settings { | |
| static let shared = Settings() | |
| private let manager = UserDefaults.standard | |
| enum List: String { | |
| case notify | |
| case nightMode | |
| } | |
| private init() { | |
| registerDefault(.notify, value: true) | |
| registerDefault(.nightMode, value: true) | |
| } | |
| private func registerDefault(_ key: List, value: Bool) { | |
| manager.register(defaults: [key.rawValue: value]) | |
| } | |
| func get(_ key: List) -> Bool { | |
| return manager.bool(forKey: key.rawValue) | |
| } | |
| func set(_ key: List, value: Bool) { | |
| manager.set(value, forKey: key.rawValue) | |
| } | |
| } | |
| // Usage | |
| let settings = Settings.shared | |
| settings.get(.notify) | |
| settings.set(.nightMode, true) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment