Created
October 12, 2015 17:10
-
-
Save s4y/8125bfa1eef872a97208 to your computer and use it in GitHub Desktop.
App defaults with an enum
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
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