<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/UserDefaults",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSUserDefaults"
  },
  "title" : "UserDefaults"
}
-->

# UserDefaults

An interface to the user’s defaults database, which stores system-wide and app-specific settings.

```
class UserDefaults
```

## Overview

A `UserDefaults` object provides access to the defaults system, which is a persistent
store for app-specific and system-wide settings. You use this system to store nonsensitive
information, such as app-specific configuration details. The system also stores configuration
details that apply to all apps, such as the current language settings for the device. In
your code, you check values from this system and use them to dynamically alter your app’s
appearance or behavior. The
term *defaults* refers to the fact that the stored data determines the default
startup state and behavior.

> Important: Don’t store personal or sensitive information as settings. The defaults
> system stores information on disk in an unencrypted format. Store personal or sensitive
> information in the person’s Keychain instead.

To access the defaults system, obtain a `UserDefaults` object and call its methods to
read and write values. The [`standard`](/documentation/Foundation/UserDefaults/standard) object is a shared object you
use to read and write your app’s standard settings. You can also create unique
`UserDefaults` objects to manage specific sets of settings. For example, you can
create a `UserDefaults` object that reads and writes settings your app shares
with an app extension. Don’t subclass `UserDefaults`.

Each item you store in a defaults object consists of a key-value pair, where each
key is a string that you use to locate the item and each value is a data object.
The defaults database supports the same value types found in property list files,
including types like <doc://com.apple.documentation/documentation/Swift/Int>,
<doc://com.apple.documentation/documentation/Swift/Float>, <doc://com.apple.documentation/documentation/Swift/Double>,
<doc://com.apple.documentation/documentation/Swift/Bool>, <doc://com.apple.documentation/documentation/Swift/String>,
[`URL`](/documentation/Foundation/URL), <doc://com.apple.documentation/documentation/Foundation/NSNumber>, [`Date`](/documentation/Foundation/Date),
<doc://com.apple.documentation/documentation/Swift/Array>, and
<doc://com.apple.documentation/documentation/Swift/Dictionary>. To include other
types of objects in the defaults database, archive them to a [`Data`](/documentation/Foundation/Data)
object first and store that object instead. Prefer simple types over custom objects whenever possible.

With the exception of managed devices in educational institutions, the system stores
defaults locally on the current device. When you write values to a `UserDefaults` object,
the object updates its in-memory version of that information right away, and writes
the value to disk asynchronously.  When someone backs up their device, the system includes
any persistent defaults databases in the backup data. Because the data is device-specific,
you don’t use the defaults system to share data between devices. To share data between
someone’s devices, use the [`NSUbiquitousKeyValueStore`](/documentation/Foundation/NSUbiquitousKeyValueStore) instead.

> Warning: Don’t access the files of the defaults database directly from the file system.
> Modifying one of the underlying files directly may cause data loss, a delay in changes
> being available, or an app crash. In macOS, use the `defaults` command-line utility to
> safely view or modify the defaults database outside of your app.

While your app is running, the defaults system generates notifications to let you know
when values change. To observe changes to individual settings, add a [key-value observer](doc://com.apple.documentation/documentation/Swift/using-key-value-observing-in-swift)
to your `UserDefaults` object, using key names to build the path to the setting
you want. To observe changes for all settings, register for a [`UserDefaults.DidChangeMessage`](/documentation/Foundation/UserDefaults/DidChangeMessage) or
[`didChangeNotification`](/documentation/Foundation/UserDefaults/didChangeNotification) with your `UserDefaults` object.

The `UserDefaults` type is thread-safe, and you can use the same object in multiple
threads or tasks simultaneously.

> Important: This API has the potential of being misused to access device signals to
> try to identify the device or user, also known as fingerprinting. Regardless of whether
> a user gives your app permission to track, fingerprinting is not allowed. When you use
> this API in your app or third-party SDK (an SDK not provided by Apple), declare your
> usage and the reason for using the API in your app or third-party SDK’s `PrivacyInfo.xcprivacy`
> file. For more information, including the list of valid reasons for using the API, see
> <doc://com.apple.documentation/documentation/BundleResources/describing-use-of-required-reason-api>.

### Domains and settings search paths

To integrate settings from different sources, the defaults system organizes them
into domains. An app defines its own custom settings, but the system defines settings
that apply to all apps. Similarly, you might choose to override a specific setting
temporarily to test one of your app’s features. The defaults system provides domains
for each of these cases along with several others.

When you request the value of a setting, the `UserDefaults` object searches its domains in
a specific order until it finds the value you want. The following table lists the key domains
that the defaults system supports and their search order. Some domains might not be
present for all apps. For example, the managed domain is present only on administrator-managed devices.

|Domain                                                                                                |Type      |Description                                                                                                                                                                                                                                                                                                                                    |
|------------------------------------------------------------------------------------------------------|----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|Managed                                                                                               |persistent|This domain contains settings that an administrator provided for a managed device. The system saves these values persistently on the current device.                                                                                                                                                                                           |
|[Argument](doc://com.apple.documentation/documentation/Foundation/UserDefaults/argumentDomain)        |volatile  |This domain contains the settings you specified when launching your app from the command-line or Xcode. These keys represent temporary overrides of settings, and the system discards them after the app quits.                                                                                                                                |
|Educational managed                                                                                   |persistent|For managed devices in an educational institution, this domain contains any settings saved to the iCloud key-value store for that institution. The system saves these settings persistently on a server, not on the device.                                                                                                                    |
|App                                                                                                   |persistent|This domain contains the settings your app saves, either programmatically or using its settings UI. Each `UserDefaults` object writes settings to this group, associating them with the app itself or the app group you used to initialize the object. The system saves these settings persistently on the current device.                     |
|Suite                                                                                                 |persistent|This domain contains custom settings from an app group or other app you specify at runtime. This domain is absent by default, but you can add a suite using the ``doc://com.apple.foundation/documentation/Foundation/UserDefaults/addSuite(named:)`` method. The system saves these settings persistently on the current device.              |
|[Global](doc://com.apple.documentation/documentation/Foundation/UserDefaults/globalDomain)            |persistent|This domain contains keys present for all apps on the system. The system provides the keys for this domain, and apps can’t write to it. The system saves these settings persistently on the current device.                                                                                                                                    |
|[Registration](doc://com.apple.documentation/documentation/Foundation/UserDefaults/registrationDomain)|volatile  |This domain contains system-provided default values and the default values you register for your app at launch time. Registering a set of default values prevents your code from receiving `nil` values when requesting a setting. The system discards these values when your app quits, so you must register them each time your app launches.|

The system stores data for most persistent domains on the current device, and doesn’t
share that data with other devices. To share settings among all of a person’s devices,
save them using an [`NSUbiquitousKeyValueStore`](/documentation/Foundation/NSUbiquitousKeyValueStore) object instead.

### Settings in managed environments

If your app supports managed environments, an administrator might configure any managed devices
with a default set of settings. For example, in a computer lab or classrom environment,
a teacher might set default settings that the lessons require. Apps can’t write to managed
domains, so if your app encounters a managed setting, disable or hide any controls that
someone might use to change that setting’s value. To determine if a setting is managed,
call the [`objectIsForced(forKey:)`](/documentation/Foundation/UserDefaults/objectIsForced(forKey:)) or [`objectIsForced(forKey:inDomain:)`](/documentation/Foundation/UserDefaults/objectIsForced(forKey:inDomain:)) method of your
`UserDefaults` object.

An app running on a managed device can use [`NSUbiquitousKeyValueStore`](/documentation/Foundation/NSUbiquitousKeyValueStore) to share
small amounts of data with the person’s other devices. Use this store for data that your app
can safely share with other instances of itself. For example, a textbook app might save the
current page number so that the person can continue reading from the same place on any of their devices.

For more details about managing devices, see <doc://com.apple.documentation/documentation/DeviceManagement>.

### Sandbox considerations

A sandboxed app cannot access or modify the settings of another app or process, with the
following exceptions:

- An app can modify settings for one of its app extensions.
- An app can modify settings for an app group to which it belongs.

If you use the [`addSuite(named:)`](/documentation/Foundation/UserDefaults/addSuite(named:)) method to add the identifier for an unrelated app, the
method doesn’t give you access to the other app’s settings. Instead, the system writes
changes to your app’s settings, not to the third-party app’s settings.

> Important: An app that accesses settings in a suite must also have the
> [App Groups entitlement](doc://com.apple.documentation/documentation/BundleResources/Entitlements/com.apple.security.application-groups).

## Topics

### Creating a user defaults object

[`standard`](/documentation/Foundation/UserDefaults/standard)

The shared defaults object for the current app.

[`init()`](/documentation/Foundation/UserDefaults/init())

Creates a new defaults object and initializes it with the app’s current settings.

[`init(suiteName:)`](/documentation/Foundation/UserDefaults/init(suiteName:))

Creates a new defaults object and initializes it with the settings from the specified database.

### Registering default settings

[`register(defaults:)`](/documentation/Foundation/UserDefaults/register(defaults:))

Specifies the set of default settings and values to use as a fallback in cases
where the app domain doesn’t have them.

### Getting the value of a key

[`bool(forKey:)`](/documentation/Foundation/UserDefaults/bool(forKey:))

Returns the Boolean value associated with the specified key.

[`integer(forKey:)`](/documentation/Foundation/UserDefaults/integer(forKey:))

Returns the integer value associated with the specified key.

[`float(forKey:)`](/documentation/Foundation/UserDefaults/float(forKey:))

Returns the floating-point value associated with the specified key.

[`double(forKey:)`](/documentation/Foundation/UserDefaults/double(forKey:))

Returns the double value associated with the specified key.

[`url(forKey:)`](/documentation/Foundation/UserDefaults/url(forKey:))

Returns the URL associated with the specified key.

[`string(forKey:)`](/documentation/Foundation/UserDefaults/string(forKey:))

Returns the string associated with the specified key.

[`stringArray(forKey:)`](/documentation/Foundation/UserDefaults/stringArray(forKey:))

Returns the array of strings associated with the specified key.

[`data(forKey:)`](/documentation/Foundation/UserDefaults/data(forKey:))

Returns the data object associated with the specified key.

[`object(forKey:)`](/documentation/Foundation/UserDefaults/object(forKey:))

Returns the object associated with the specified key.

[`array(forKey:)`](/documentation/Foundation/UserDefaults/array(forKey:))

Returns the array associated with the specified key.

[`dictionary(forKey:)`](/documentation/Foundation/UserDefaults/dictionary(forKey:))

Returns the dictionary object associated with the specified key.

[`dictionaryRepresentation()`](/documentation/Foundation/UserDefaults/dictionaryRepresentation())

Returns a dictionary with the union of all key-value pairs found from all domains.

### Setting the value for a key

[`set(_:forKey:)`](/documentation/Foundation/UserDefaults/set(_:forKey:)-3nn5m)

Sets the value of the specified key to a Boolean value.

[`set(_:forKey:)`](/documentation/Foundation/UserDefaults/set(_:forKey:)-3v852)

Sets the value of the specified key to an integer.

[`set(_:forKey:)`](/documentation/Foundation/UserDefaults/set(_:forKey:)-1t5ec)

Sets the value of the specified key to a floating-point number.

[`set(_:forKey:)`](/documentation/Foundation/UserDefaults/set(_:forKey:)-2w22f)

Sets the value of the specified key to a double.

[`set(_:forKey:)`](/documentation/Foundation/UserDefaults/set(_:forKey:)-2bqjt)

Sets the value of the specified key to a URL.

[`set(_:forKey:)`](/documentation/Foundation/UserDefaults/set(_:forKey:)-8ab6d)

Sets the value of the specified key to a property list object.

### Monitoring settings changes and issues

[`UserDefaults.DidChangeMessage`](/documentation/Foundation/UserDefaults/DidChangeMessage)

A message the system sends when a user-defaults setting changes.

[`didChangeNotification`](/documentation/Foundation/UserDefaults/didChangeNotification)

Posted when the current process changes the value of a setting.

[`UserDefaults.SizeLimitExceededMessage`](/documentation/Foundation/UserDefaults/SizeLimitExceededMessage)

A message the system sends when the size of the data in the defaults database exceeds the maximum.

[`sizeLimitExceededNotification`](/documentation/Foundation/UserDefaults/sizeLimitExceededNotification)

Posted when the amount of data in the defaults database exceeds the allowed maximum.

### Removing settings values

[`removeObject(forKey:)`](/documentation/Foundation/UserDefaults/removeObject(forKey:))

Removes the value for the specified key from the defaults database.

### Adding and removing search domains

[`addSuite(named:)`](/documentation/Foundation/UserDefaults/addSuite(named:))

Inserts settings for the specified domain into the search list of the current object.

[`removeSuite(named:)`](/documentation/Foundation/UserDefaults/removeSuite(named:))

Removes the specified domain from the search list of the current object.

### Getting the domain names

[`argumentDomain`](/documentation/Foundation/UserDefaults/argumentDomain)

The identifier for the domain that contains command-line settings.

[`globalDomain`](/documentation/Foundation/UserDefaults/globalDomain)

The identifier for the domain that contains system-specified settings for all apps.

[`registrationDomain`](/documentation/Foundation/UserDefaults/registrationDomain)

The identifier for the domain that contains your app’s registered default values.

[`volatileDomainNames`](/documentation/Foundation/UserDefaults/volatileDomainNames)

An array of identifiers for the volatile domains associated with the current object.

### Managing domain-specific values

[`persistentDomain(forName:)`](/documentation/Foundation/UserDefaults/persistentDomain(forName:))

Retrieves the settings from the specified persistent domain.

[`setPersistentDomain(_:forName:)`](/documentation/Foundation/UserDefaults/setPersistentDomain(_:forName:))

Replaces the keys and values in the specified domain with the new keys and values you supply.

[`volatileDomain(forName:)`](/documentation/Foundation/UserDefaults/volatileDomain(forName:))

Retrieves the settings from the specified volatile domain.

[`setVolatileDomain(_:forName:)`](/documentation/Foundation/UserDefaults/setVolatileDomain(_:forName:))

Replaces the keys and values in the specified domain with the new keys and values you supply.

[`removePersistentDomain(forName:)`](/documentation/Foundation/UserDefaults/removePersistentDomain(forName:))

Removes the keys and values from the specified persistent domain.

[`removeVolatileDomain(forName:)`](/documentation/Foundation/UserDefaults/removeVolatileDomain(forName:))

Removes the keys and values from the specified volatile domain.

### Checking for managed keys

[`objectIsForced(forKey:)`](/documentation/Foundation/UserDefaults/objectIsForced(forKey:))

Returns a Boolean value that indicates whether an administrator provided the
value for the specified key.

[`objectIsForced(forKey:inDomain:)`](/documentation/Foundation/UserDefaults/objectIsForced(forKey:inDomain:))

Returns a Boolean value that indicates whether an administrator provided the value
for the key in the specified domain.

### Deprecated

[`init(user:)`](/documentation/Foundation/UserDefaults/init(user:))

Creates a user defaults object initialized with the defaults for the specified user account.

[`synchronize()`](/documentation/Foundation/UserDefaults/synchronize())

Waits for any pending asynchronous updates to the defaults database and returns; this method is unnecessary and shouldn’t be used.

[`resetStandardUserDefaults()`](/documentation/Foundation/UserDefaults/resetStandardUserDefaults())

This method has no effect and shouldn’t be used.

[`persistentDomainNames()`](/documentation/Foundation/UserDefaults/persistentDomainNames())

Returns an array of the current persistent domain names.

[`completedInitialCloudSyncNotification`](/documentation/Foundation/UserDefaults/completedInitialCloudSyncNotification)

Posted when ubiquitous defaults finish downloading data, either the first time a device is connected to an iCloud account or when a user switches their primary iCloud account.

[`didChangeCloudAccountsNotification`](/documentation/Foundation/UserDefaults/didChangeCloudAccountsNotification)

Posted when the user changes the primary iCloud account.

[`noCloudAccountNotification`](/documentation/Foundation/UserDefaults/noCloudAccountNotification)

Posted when a cloud default is set, but no iCloud user is logged in.

[Language-Dependent Information Constants](/documentation/Foundation/language-dependent-information-constants)

These constants are deprecated and shouldn’t be used.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)