Last active
July 19, 2024 15:14
-
-
Save jasdev/cd96d61277a8b1c1e6f4b413d9ccc645 to your computer and use it in GitHub Desktop.
Swift nonmutating Example
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 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