Is there a way to get all values in NSUserDefaults?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Yes. On iOS, the standard way to inspect all values currently visible in NSUserDefaults or UserDefaults is to ask for its dictionary representation. That gives you a snapshot of the defaults domain as key-value pairs. The part that matters in real apps is understanding what is included and how to separate your app’s keys from framework or shared-suite data.
Use dictionaryRepresentation() In Swift
In modern Swift, the most direct API is:
This returns a [String: Any] dictionary containing the defaults visible to that UserDefaults instance.
That is the correct “get everything” answer for inspection, debugging, migration checks, or building lightweight settings screens.
Objective-C Equivalent
If you are working in Objective-C, the equivalent call is:
The concept is the same: you get a dictionary snapshot of the defaults that the instance knows about.
What You Actually Get Back
This is where people sometimes get surprised. dictionaryRepresentation() is not just a dump of the exact keys you remember writing manually. It can include:
- your app’s stored preferences,
- registered defaults,
- framework-related entries,
- values from the selected defaults domain or suite.
So it is useful, but it is not always a perfectly clean “app-only settings” dictionary.
Filtering Only Your App’s Keys
A practical pattern is to namespace your own keys with a prefix and then filter.
If your keys are named like app.theme, app.username, and app.notifications.enabled, filtering becomes easy and future-proof.
Without a naming convention, the result dictionary can be harder to reason about.
Reading Specific Values Is Still Better For Normal Code
Although you can get all values, ordinary application code should still prefer typed accessors for known keys.
Typed reads are safer than pulling values from [String: Any] and casting manually everywhere.
So dictionaryRepresentation() is best seen as an inspection and utility API, not as the main way to access defaults during everyday app logic.
App Group And Shared Defaults
If your app uses extensions such as widgets or share extensions, you may be reading from a suite rather than the standard defaults domain.
This is important because UserDefaults.standard and an app-group suite are not the same storage scope.
When This Is Useful
Common use cases include:
- debugging preference migrations,
- verifying which keys exist after onboarding,
- exporting or resetting app settings,
- checking app-group values during extension development.
It is especially helpful in development builds when you want a quick snapshot of the current defaults state.
When It Is The Wrong Tool
UserDefaults is meant for small preference-style data. If you are trying to use “get all values” because you stored lots of app data there, the storage design is probably off.
Do not use UserDefaults as a substitute for:
- a database,
- large cached payloads,
- secret storage,
- document persistence.
For secrets, use Keychain. For structured app data, use a database or files.
Common Pitfalls
- Assuming
dictionaryRepresentation()returns only your app-defined keys. - Using the dictionary snapshot as the primary read path instead of typed accessors.
- Forgetting that app-group suites and standard defaults are separate domains.
- Storing large or sensitive data in
UserDefaultsjust because it is easy to enumerate. - Skipping a key-naming convention and making defaults harder to inspect later.
Summary
- Use
UserDefaults.standard.dictionaryRepresentation()to get all currently visible defaults values. - The Objective-C equivalent is
[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]. - Filter by key prefix if you want only your app’s entries.
- Prefer typed accessors for normal reads of known values.
- Use
UserDefaultsfor small preferences, not for secrets or large datasets.
Related reading
- Is there a way to get the source code from an APK file?
- Is there a way to get the source code from an APK file?
- Is there a way to hide the scroll indicators in a UIScrollView?
- Is there a way to pretty print Swift dictionaries to the console?
- Is there a way to pretty print Swift dictionaries to the console?
- Is there a way to programmatically scroll a scroll view to a specific edit text?
- Is there a way to run Python on Android?
- Is there a way to run Python on Android?
.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.