Skip to content

Instantly share code, notes, and snippets.

@s4y
Created October 12, 2015 17:10
Show Gist options
  • Save s4y/8125bfa1eef872a97208 to your computer and use it in GitHub Desktop.
Save s4y/8125bfa1eef872a97208 to your computer and use it in GitHub Desktop.
App defaults with an enum
enum AppDefault {
case APIServer, RunMode, HomeDirectory, ReactHost
var stringValue: String? {
get {
return NSUserDefaults.standardUserDefaults().stringForKey(String(self))
}
nonmutating set {
if let newValue = newValue {
NSUserDefaults.standardUserDefaults().setObject(newValue, forKey: String(self))
} else {
NSUserDefaults.standardUserDefaults().removeObjectForKey(String(self))
}
}
}
}
extension String {
init(_ appDefault: AppDefault) {
switch appDefault {
case .APIServer: self = "APIServer"
case .RunMode: self = "RunMode"
case .HomeDirectory: self = "HomeDirectory"
case .ReactHost: self = "ReactHost"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment