skip to content

A Core Data model logs that a Transformable attribute is using nil or an insecure NSValueTransformer; what does that mean, and how do you store a custom watering schedule safely?

level: seniorimportance: should knowfreq 42%

answer

  1. how a Transformable becomes bytes
  2. nil transformer name means NSCoding
  3. secure coding limits the classes
  4. allowedTopLevelClasses on a subclass
  5. register before the stack loads

basics

~10 s

A Transformable attribute with no transformer, or a deprecated keyed-unarchive one, unarchives whatever classes the data names. Name NSSecureUnarchiveFromData for Foundation values, or register an NSSecureUnarchiveFromDataTransformer subclass listing your NSSecureCoding class in allowedTopLevelClasses.

solid answer

~40 s

A **Transformable** attribute stores an object as `Data` through a `ValueTransformer` named by `valueTransformerName`. Left nil, Core Data falls back to an NSCoding archiver that will decode any class the data names, so it warns at build and at launch. For arrays or dictionaries of strings, numbers, dates, URLs or UUIDs, set the transformer to **NSSecureUnarchiveFromData**. For the schedule, make `WateringSchedule` an `NSObject` that adopts `NSSecureCoding`, subclass `NSSecureUnarchiveFromDataTransformer`, override `allowedTopLevelClasses` to return it, register it with `ValueTransformer.setValueTransformer(_:forName:)` before the container loads, and name it plus the custom class in the inspector. The archive stays opaque, so predicates cannot reach its fields; on iOS 17 and later a **composite attribute** can describe plain fields to Core Data instead.

code

plaintext · 1 line
plaintext
CoreData: warning: Property 'schedule' on Entity 'Plant' is using nil or an insecure NSValueTransformer.  Please switch to using "NSSecureUnarchiveFromData" or a subclass of NSSecureUnarchiveFromDataTransformer instead.

go deeper

for a junior

Recall that a Transformable attribute turns an object into data through a named value transformer, and that an empty name triggers a security warning.

for a middle

Explain the secure transformer subclass, allowedTopLevelClasses, and registering the transformer before the persistent container loads.

for a senior

Plan for archives that outlive the code: stable @objc class names, tolerant decoding of older rows, and fields kept queryable where they matter.

for a principal

Weigh opaque archives against modelled attributes as a schema policy, since each representation change later costs a migration.

## How a Transformable attribute is stored Core Data stores a fixed set of attribute types natively: integers, floating-point numbers, strings, dates, binary data, UUIDs, URIs and a few more. A **Transformable** attribute holds any other object by converting it to `Data` on the way into the store and back on the way out. The conversion is a `ValueTransformer` (Foundation's `NSValueTransformer`) that `NSAttributeDescription.valueTransformerName` names; the attribute documentation says the transformer must output `Data` and allow reverse transformation. `attributeValueClassName` (the inspector's **Custom Class**) tells the code generator which Swift type to give the property; without it the property is `NSObject?`. When `valueTransformerName` is nil, Core Data uses a default transformer built on NSCoding archiving. Plain keyed unarchiving re-creates whatever classes the archive names, so crafted data could make the app instantiate an unexpected class. Foundation deprecated `NSKeyedUnarchiveFromDataTransformerName` and `NSUnarchiveFromDataTransformerName` in iOS 12 in favour of `NSSecureUnarchiveFromDataTransformerName`, and Core Data now warns about both cases: the model compiler at build time and the framework at launch. ```plaintext CoreData: warning: Property 'schedule' on Entity 'Plant' is using nil or an insecure NSValueTransformer. Please switch to using "NSSecureUnarchiveFromData" or a subclass of NSSecureUnarchiveFromDataTransformer instead. ``` The old transformer still reads and writes existing values; the warning is about what it is willing to decode. Read it as three facts: - the **property and entity** it names (`schedule` on `Plant`) are the attribute to fix; - **nil or insecure** covers both an empty transformer name and the deprecated keyed-unarchive names; - the **suggested fix** is the secure transformer name, or a subclass of `NSSecureUnarchiveFromDataTransformer` for your own classes. The same attribute also draws a model-compiler warning in the build log, so it is visible before anyone runs the app. ## Fixing it by what the value is 1. **Foundation values only** (a list of tags, a dictionary of strings to numbers): set the transformer name to `NSSecureUnarchiveFromData`. `NSSecureUnarchiveFromDataTransformer` accepts `NSArray`, `NSDictionary`, `NSSet`, `NSString`, `NSNumber`, `NSDate`, `NSData`, `NSURL`, `NSUUID` and `NSNull` at the top level. 2. **Your own class**: adopt `NSSecureCoding` (set `supportsSecureCoding` to true and decode with the typed `decodeObject(of:forKey:)` family), subclass `NSSecureUnarchiveFromDataTransformer`, and override the class property `allowedTopLevelClasses` to list your class. 3. **Register before loading.** The transformer documentation says to register your subclass before initializing the persistent container; `ValueTransformer.setValueTransformer(_:forName:)` at launch, before `loadPersistentStores`, is the usual place. 4. **Name both in the model**: the transformer name and the custom class, so the property is typed `WateringSchedule?`. A Swift `struct` cannot adopt `NSSecureCoding`. The choices are to model it as an `NSObject` subclass as above; to write a `ValueTransformer` subclass that encodes the struct to `Data` and back; or to store it as plain Binary Data and encode it in your own code. ## Keeping archived values readable - Give the archived class a fixed runtime name with `@objc(WateringSchedule)`. A keyed archive records class names, and a Swift class otherwise records a module-qualified name that changes if the module is renamed. - Evolve `init?(coder:)` with older archives in mind: treat keys added later as optional when decoding, so rows written by an earlier release still decode. - Remember the value is opaque to the store: a fetch request cannot filter on `millilitres` inside the archive, and changing one field rewrites the whole value. ## Composite attributes as the alternative Since iOS 17, `NSCompositeAttributeDescription` (attribute type `.composite`) groups several ordinary attributes under one attribute name; its `elements` must be attributes, and setting a relationship among them throws. When the schedule is just weekdays and an amount, describing them as elements keeps the fields visible to Core Data rather than hidden in an archive. On an iOS 17 baseline it is available on every supported device. ## Choosing between them | Value | Store it as | |---|---| | Array or dictionary of Foundation types | Transformable with `NSSecureUnarchiveFromData` | | Your class with behaviour | Transformable with a registered secure transformer subclass | | A few plain fields Core Data should see | composite attribute (iOS 17), or separate attributes | | Large media such as the plant photo | Binary Data with Allows External Storage | Whatever the choice, it is a schema decision: moving a value from one representation to another later means a migration, covered in the lesson on model versioning and migration.

  • Why does a Core Data transformer subclass need allowedTopLevelClasses overridden?
    Secure unarchiving refuses any top-level class not on the allowed list. The base class allows only Foundation types such as `NSArray`, `NSString` and `NSNumber`, so a `WateringSchedule` archive fails to decode until the subclass returns `[WateringSchedule.self]`.
  • Can a Core Data fetch request filter plants by a field inside a Transformable schedule?
    No. The store holds the schedule as archived bytes, so a predicate cannot reach `millilitres` inside it. Store any field you filter on as its own attribute, and keep only what you never query inside the archive.
  • Why give a Core Data archived class an explicit @objc name?
    The keyed archive records the class name. A Swift class's runtime name includes its module, so renaming the module or target leaves old rows naming a class that no longer exists; `@objc(WateringSchedule)` keeps the recorded name stable.

  • Leaving the transformer name empty because the values still load
  • Believing NSKeyedUnarchiveFromData is the secure replacement
  • Making a Swift struct adopt NSSecureCoding directly
  • Expecting predicates to filter on fields inside a Transformable value
  • Forgetting to list the custom class in allowedTopLevelClasses
  • Storing large photos as Transformable instead of external Binary Data

Assumes an iOS 17 or later deployment target: NSSecureUnarchiveFromDataTransformer arrived in iOS 12 and composite attributes in iOS 17.