UserDefaults
Also known as: nsuserdefaults, plist preferences ios, ios userdefaults
UserDefaults is Apple's lightweight key-value store for persisting small pieces of user preference and app configuration data, backed by a property list (.plist) file in the app container. It is meant for settings, not bulk data or caches.
- UserDefaults persists to a property list (.plist) file in the app's Library/Preferences directory.
- It is intended for small settings and preferences, not large blobs or caches.
- Unlike cache directories, its data is not auto-purged under memory or storage pressure.
How UserDefaults works
UserDefaults (formerly NSUserDefaults) stores simple key-value pairs such as flags, strings, numbers, dates, and small `Data` blobs. You read and write through `UserDefaults.standard`, and the system persists the values to a property list file inside the app's `Library/Preferences` directory. Writes are cached in memory and flushed to disk periodically, so changes survive app relaunches.
It is the right tool for things like a user's chosen theme, the last-opened tab, or a 'has seen onboarding' flag, data that is small, structured, and tied to the app or user identity.
UserDefaults vs cache, and the bloat anti-pattern
Unlike a clearable cache, UserDefaults data is treated as preferences and is not auto-evicted under memory or storage pressure, it persists until the app deletes it or the app is uninstalled. Storing large blobs (images, big JSON payloads) here is an anti-pattern: it bloats the preferences plist, slows startup because the whole store loads into memory, and the data cannot be reclaimed like cache files in NSCachesDirectory.
For transient or recreatable data, developers should use a real cache (NSCache, URLCache, or the caches directory) so the system and storage cleaners can reclaim it, while keeping UserDefaults for genuinely small settings.