Skip to content

Instantly share code, notes, and snippets.

@jasdev
Last active June 14, 2019 10:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasdev/cd96d61277a8b1c1e6f4b413d9ccc645 to your computer and use it in GitHub Desktop.
Save jasdev/cd96d61277a8b1c1e6f4b413d9ccc645 to your computer and use it in GitHub Desktop.
Swift nonmutating Example
import Foundation
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))
}
}
}
}
let server = AppDefault.APIServer
// `AppDefault`'s `nonmutating` setter on `stringValue` allows for this work on immutable instances
server.stringValue = "127.0.0.1:4000/"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment