Attempt to set a non-property-list object as an NSUserDefaults
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
This error means you tried to store a value in UserDefaults that is not one of the supported property-list types. UserDefaults is meant for small preference-like values, not arbitrary Swift objects or rich model graphs.
What UserDefaults Can Store Directly
Directly supported values include:
- '
String' - '
Int,Double,Bool, and other scalar wrappers' - '
Date' - '
Data' - arrays containing supported values
- dictionaries containing supported values
A custom struct or class is not supported automatically.
This fails:
The error appears because Settings is not a property-list type.
Encode Custom Types First
The normal modern fix is to make the type Codable and store the encoded data:
Reading the value back requires decoding:
This works because Data is a supported type even though the original object is not.
A Dictionary Works Only for Simple Values
If the data is tiny and flat, you can sometimes store a dictionary:
This is acceptable only when every nested value is also property-list compatible. It becomes fragile quickly because everything is string-keyed and type safety is weak.
UserDefaults Is Not a General Database
A common design mistake is using UserDefaults as lightweight object persistence for everything. It is a better fit for:
- theme settings
- selected options
- feature flags
- small user preferences
It is a poor fit for:
- large object graphs
- image blobs
- caches
- queryable data
- anything needing schema migration beyond simple preferences
If the value is large or structurally rich, use files, Core Data, SQLite, or another persistence layer.
JSONEncoder vs PropertyListEncoder
Both are reasonable because the final stored value is Data. JSONEncoder is often convenient because the bytes are easier to inspect during debugging. PropertyListEncoder is also fine if you want an Apple-style plist representation.
Example with PropertyListEncoder:
The important rule is not the encoder choice. It is that the value you write into UserDefaults must be a supported type.
Old Name, Same Rules
The older Objective-C error text mentions NSUserDefaults, while modern Swift usually uses UserDefaults. The storage rules are the same. The class naming style changed, but the requirement for property-list compatibility did not.
Common Pitfalls
- Trying to store a custom struct or class directly instead of encoding it first.
- Building a dictionary that looks valid at the top level but contains unsupported nested objects.
- Treating
UserDefaultsas a general persistence layer for large or complex data. - Forgetting to handle decode failures when the stored format changes over time.
- Using
[String: Any]everywhere and losing type safety when aCodablemodel would be clearer.
Summary
- '
UserDefaultsstores only property-list compatible values directly.' - Custom objects must be encoded into a supported type such as
Data. - '
CodablewithJSONEncoderorPropertyListEncoderis the usual modern fix.' - Small dictionaries are fine only when every nested value is also supported.
- Use
UserDefaultsfor lightweight preferences, not for general-purpose object storage.
Related reading
- AttributeError list object has no attribute add
- Auto-initializing C lists
- Autocomplete using a trie
- Average values of dictionaries
- Attempting to load the view of a view controller while it is deallocating... UISearchController
- Attempting to load the view of a view controller while it is deallocating... UISearchController
- Attempted to read or write protected memory. This is often an indication that other memory is corrupt
- Attempted to read or write protected memory. This is often an indication that other memory is corrupt

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.