Created
April 4, 2021 06:40
-
-
Save xmhafiz/bfe4a0ef14fa4367f8169a16ce80d074 to your computer and use it in GitHub Desktop.
CustomUserDefaultsFull.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
| // CustomUserDefaults.swift (full) | |
| import Foundation | |
| class CustomUserDefaults { | |
| // define all keys needed | |
| enum DefaultsKey: String, CaseIterable { | |
| case name | |
| case email | |
| case isUserLogin | |
| case userLevel | |
| } | |
| static let shared = CustomUserDefaults() | |
| private let defaults = UserDefaults.standard | |
| init() {} | |
| // to set value using pre-defined key | |
| func set(_ value: Any?, key: DefaultsKey) { | |
| defaults.setValue(value, forKey: key.rawValue) | |
| } | |
| // get value using pre-defined key | |
| func get(key: DefaultsKey) -> Any? { | |
| return defaults.value(forKey: key.rawValue) | |
| } | |
| // check value if exist or nil | |
| func hasValue(key: DefaultsKey) -> Bool { | |
| return defaults.value(forKey: key.rawValue) != nil | |
| } | |
| // remove all stored values | |
| func removeAll() { | |
| for key in DefaultsKey.allCases { | |
| defaults.removeObject(forKey: key.rawValue) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment