*NSUserDefaults is a class in the iOS and macOS SDKs that provides a lightweight way to persistently store small amounts of data, such as user preferences and settings, across app launches. *
In Xcode, you can use NSUserDefaults to store and retrieve key-value pairs in the application's default user preferences database. This database is a property list file that is automatically created and managed by the system.
Here's an example of how to use NSUserDefaults to store and retrieve a string value:
To store a string value:
let defaults = UserDefaults.standard
defaults.set("Hello, World!", forKey: "myString")To retrieve a string value:
let defaults = UserDefaults.standard
if let myString = defaults.string(forKey: "myString") {
print(myString) // prints "Hello, World!"
}Note that UserDefaults.standard returns a shared instance of NSUserDefaults that you can use throughout your app. You can also create your own NSUserDefaults instance if you need to manage separate preference domains.