Created
March 25, 2022 13:17
-
-
Save ekscrypto/24c9beead5fb6e4c396a26c3708197c4 to your computer and use it in GitHub Desktop.
Basic TypeSafeUserDefaults
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 TypeSafeUserDefaults { | |
enum IntegerKey: StringLiteralType, RawRepresentable { | |
case myIntegerValue | |
} | |
enum StringKey: StringLiteralType, RawRepresentable { | |
case myStringValue | |
} | |
// Integer | |
func integer(forKey key: IntegerKey) -> Int { | |
UserDefaults.standard.integer(forKey: key.rawValue) | |
} | |
func set(_ integer: Int, forKey key: IntegerKey) { | |
UserDefaults.standard.set(integer, forKey: key.rawValue) | |
} | |
// String | |
func string(forKey key: StringKey) -> String? { | |
UserDefaults.standard.string(forKey: key.rawValue) | |
} | |
func set(_ string: String, forKey key: StringKey) { | |
UserDefaults.standard.set(string, forKey: key.rawValue) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment