Skip to content

Instantly share code, notes, and snippets.

@jasdev
Last active July 19, 2024 15:14
Show Gist options
  • 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