NSUserDefaults
iOS
Swift
Objective-C
duplicate

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.

Browse interview questions

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:

swift
let allValues = UserDefaults.standard.dictionaryRepresentation()
print(allValues)

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:

objective-c
NSDictionary *allValues = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
NSLog(@"%@", allValues);

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.

swift
1let appValues = UserDefaults.standard
2    .dictionaryRepresentation()
3    .filter { key, _ in
4        key.hasPrefix("app.")
5    }
6
7print(appValues)

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.

swift
1let defaults = UserDefaults.standard
2let username = defaults.string(forKey: "app.username")
3let launchCount = defaults.integer(forKey: "app.launchCount")
4let isPremium = defaults.bool(forKey: "app.isPremium")

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.

swift
1if let sharedDefaults = UserDefaults(suiteName: "group.com.example.app") {
2    let values = sharedDefaults.dictionaryRepresentation()
3    print(values)
4}

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 UserDefaults just 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 UserDefaults for small preferences, not for secrets or large datasets.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.