Save string to the NSUserDefaults?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Saving a string to UserDefaults is one of the simplest kinds of iOS persistence. It works well for lightweight preferences and small pieces of state, but it is the wrong place for secrets, large data, or anything that behaves more like application data than a user preference.
Basic Save and Read Pattern
In modern Swift, use UserDefaults.standard:
This stores a string under a stable key and retrieves it later.
Prefer Stable Keys and Small Helpers
Raw string keys scattered throughout the codebase lead to typos and migration problems. A helper keeps things centralized:
This also makes it easier to rename or migrate keys later.
Handle Missing Values Intentionally
A missing key is not always the same thing as an empty string. Sometimes you want a fallback:
And sometimes you want to know whether the value was ever set:
Choosing deliberately between optional behavior and a default value keeps UI and analytics logic clearer.
Update and Remove Values
Writing the same key again updates the stored value:
To remove the key:
This is common in reset flows and logout handling.
Know What Belongs in UserDefaults
Good candidates:
- onboarding flags
- last selected tab or filter
- theme preferences
- non-sensitive small strings such as a display preference
Bad candidates:
- passwords or tokens
- large text blobs
- images
- frequently changing high-volume data
Use the Keychain for secrets and a file or database layer for larger or more structured data.
App Groups for Shared Values
If an app and its extension need to share the same value, use a suite backed by an app group:
Without the app group configuration, the main app and extension have separate defaults containers.
Testing and Key Migration
In tests, avoid polluting the real defaults store. Use a suite-specific store instead:
When renaming keys, migrate once:
That avoids silently losing settings during app updates.
Common Pitfalls
- Storing secrets in
UserDefaultsinstead of the Keychain. - Scattering raw string keys across the codebase and introducing typos.
- Treating missing values and empty strings as if they always mean the same thing.
- Storing large or frequently changing data and turning a preference store into a poor database.
- Forgetting suite configuration when data must be shared with extensions.
Summary
- '
UserDefaultsis fine for lightweight non-sensitive strings and preferences.' - Save with
set(_:forKey:)and read with typed accessors such asstring(forKey:). - Centralize keys instead of duplicating raw string literals everywhere.
- Use Keychain for secrets and other storage for larger data.
- Use app-group suites when app extensions need to share values.
Related reading
- Save string to the NSUserDefaults?
- Save Struct to UserDefaults
- Saving and Reading Bitmaps/Images from Internal memory in Android
- Scala Programming for Android
- scale Image in an UIButton to AspectFit?
- Scale Image to fill ImageView width and keep aspect ratio
- Scale UIButton Animation- Swift
- SceneKit - Threads - What to do on which thread?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.