Introduction: When you open a setting in an app—like turning on Dark Mode or muting game sounds—you expect the app to remember your choice next time you open it. If the app forgets, it is annoying. In iOS, the easiest way to save these small settings is using UserDefaults.
The Analogy: Sticky Notes on the Fridge
Imagine your kitchen. If you need to remember something small, like 'Buy milk' or 'Turn on light', you write it on a sticky note and stick it on the fridge. It is fast, simple, and you see it instantly.
But if you have a massive library of 500 books, you would not write them all on sticky notes. You would use a big bookshelf (which in iOS coding is a database like **Core Data**).
In iOS, UserDefaults is the fridge where you stick small notes. It is a simple key-value store. You save a value (like true) under a key name (like "isDarkModeEnabled").
What Can You Save in UserDefaults?
UserDefaults is designed for small, simple data types. You can save:
- Booleans:
trueorfalse(perfect for toggle switches). - Numbers: Integers (like high scores
150) and Doubles (like volumes0.8). - Strings: Text names (like username
"Alex"). - Arrays: Simple lists of items (like a list of high scores).
How to Save and Load Data in Code
Writing and reading from UserDefaults takes just one line of Swift code. Let's look at the basic syntax:
1. Saving a Setting (Writing)
// Save that the user wants Dark Mode
UserDefaults.standard.set(true, forKey: "isDarkModeEnabled")
// Save the user's favorite volume level
UserDefaults.standard.set(0.85, forKey: "appVolume")
// Save a username
UserDefaults.standard.set("Alex Swift", forKey: "username")2. Loading a Setting (Reading)
// 1. Read a boolean (default is false if key doesn't exist)
let isDark = UserDefaults.standard.bool(forKey: "isDarkModeEnabled")
print("Dark Mode is: \(isDark)")
// 2. Read a double (default is 0.0 if key doesn't exist)
let volume = UserDefaults.standard.double(forKey: "appVolume")
print("Volume is: \(volume)")
// 3. Read a string (returns nil if key doesn't exist, so we use '??')
let name = UserDefaults.standard.string(forKey: "username") ?? "Guest"
print("Hello, \(name)!")3. Removing a Setting (Deleting)
// If you want to reset a setting, just remove the key
UserDefaults.standard.removeObject(forKey: "username")Common Pitfalls & Warnings
While UserDefaults is very easy, it is easy to abuse. Here are three rules to keep in mind:
2. Keep it small: Do not save large files, PDFs, or images here. Loading them will slow down your app startup time.
3. Watch out for typos: If you write
"isDarkMode" when saving, and try to read "isDarkmode" (lowercase m) when loading, it will fail. Keys are case-sensitive!iOS Storage: Where Should I Put My Data?
To help you choose the right tool for saving files, use this table:
| Storage Type | Best For | Security Level | Complexity |
|---|---|---|---|
| UserDefaults | Preferences, Settings, Flags | Low (Unencrypted) | Very Easy |
| Keychain | Passwords, Access Tokens, PINs | High (Encrypted) | Medium |
| Core Data | Large databases, relationships | Medium | Hard |
Summary
UserDefaults is the perfect place to store simple things like theme choices, game high scores, and user preferences. Just remember: keep the data small, use consistent keys, and never store passwords. With these simple rules, you can make your app remember settings like a pro!