Skip to content

Latest commit

 

History

History
27 lines (18 loc) · 1.4 KB

NSUserDeafault.md

File metadata and controls

27 lines (18 loc) · 1.4 KB

NSUserDeafaults

*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.

It's important to keep in mind that NSUserDefaults is not intended to be used as a general-purpose database. It's designed for storing small amounts of data, such as user preferences and settings. If you need to store larger amounts of data or more complex data structures, you should consider using a database or other persistent storage mechanism.