Created
March 22, 2017 15:04
-
-
Save ArtSabintsev/28e889cfd361d6b11f2029ca5570812d to your computer and use it in GitHub Desktop.
An example on a couple ways to create keys for UserDefaults.
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
import Foundation | |
enum EnumKey: String { | |
case aKey = "Key A from Enum" | |
case bKey = "Key B from Enum" | |
case cKey = "Key C from Enum" | |
} | |
struct StructKey { | |
static let aKey = "Key A from Struct" | |
static let bKey = "Key B from Struct" | |
static let cKey = "Key C from Struct" | |
} | |
/// ENUM | |
// Store to Defaults | |
UserDefaults.standard.set(true, forKey: EnumKey.aKey.rawValue) | |
UserDefaults.standard.set(false, forKey: EnumKey.bKey.rawValue) | |
UserDefaults.standard.set(false, forKey: EnumKey.cKey.rawValue) | |
// Fetch from Defaults | |
UserDefaults.standard.bool(forKey: EnumKey.aKey.rawValue) | |
UserDefaults.standard.bool(forKey: EnumKey.bKey.rawValue) | |
UserDefaults.standard.bool(forKey: EnumKey.cKey.rawValue) | |
/// STRUCT + STATIC LET | |
// Fetch from Defaults | |
UserDefaults.standard.set(false, forKey: StructKey.aKey) | |
UserDefaults.standard.set(true, forKey: StructKey.bKey) | |
UserDefaults.standard.set(true, forKey: StructKey.cKey) | |
// Store to Defaults | |
UserDefaults.standard.bool(forKey: StructKey.aKey) | |
UserDefaults.standard.bool(forKey: StructKey.bKey) | |
UserDefaults.standard.bool(forKey: StructKey.cKey) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, this is very useful. Here's a little riff I did, but it's hard to think about getting much leverage with my approach:
Unfortunately, there's no way to get inheritance for Enums, otherwise this would be much better ;)