Why this happens
Core Data does not have a native array type. When you need to persist a collection of values — such as a list of Double measurements — you must choose a storage strategy. The simplest built-in option is a Transformable attribute, which uses NSCoding (or NSSecureCoding) to serialize the array into a Data blob in the SQLite store. At runtime Core Data deserializes it back into a Swift array.
The key is configuring the model correctly: the attribute type must be Transformable and the Custom Class field must match the concrete array type you intend to store ([Double], [NSNumber], [String], etc.). If the Custom Class is left blank or set to NSArray, you will get an NSArray back instead of a typed Swift array, forcing manual casting.
How to fix it
1. Configure the attribute in the data model
- Open your
.xcdatamodeldfile. - Select the entity (e.g.,
PBOStatistics). - Add a new attribute named
values. - Set Type to Transformable.
- In the Custom Class field, enter
[Double](or[NSNumber]if you prefer Foundation numbers).
Note: The Custom Class must be written exactly as the Swift type name including brackets. Do not add a module prefix.
2. Declare the property in the NSManagedObject subclass
import CoreData
final class PBOStatistics: NSManagedObject {
@NSManaged var date: Date
@NSManaged var average: NSNumber
@NSManaged var historicAverage: NSNumber
@NSManaged var total: NSNumber
@NSManaged var historicTotal: NSNumber
@NSManaged var ordersCount: NSNumber
@NSManaged var historicOrdersCount: NSNumber
@NSManaged var values: [Double] // matches Custom Class in model
@NSManaged var location: PBOLocation
}
// If you use MagicalRecord or another helper, the MR_createInContext
// call remains unchanged.
3. Save and read the array normally
let context = NSManagedObjectContext.mr_default() // or your context
let statistics = PBOStatistics.mr_create(in: context)!
statistics.values = [8, 17.7, 18, 21, 0, 0, 34]
try? context.mr_saveToPersistentStoreAndWait()
// Reading back
let saved = statistics.values // [Double]
print(saved) // [8.0, 17.7, 18.0, 21.0, 0.0, 0.0, 34.0]
Core Data handles the encoding/decoding transparently. The array is stored as a single BLOB column in SQLite.
4. Alternative: Binary Data attribute with manual encoding
If you need more control (e.g., custom compression, encryption, or JSON export), declare the attribute as Binary Data in the model and do the encoding yourself:
// Model: attribute 'valuesData' of type Binary Data
@NSManaged var valuesData: Data
var values: [Double] {
get {
(try? JSONDecoder().decode([Double].self, from: valuesData)) ?? []
}
set {
valuesData = (try? JSONEncoder().encode(newValue)) ?? Data()
}
}
This approach avoids NSCoding and makes the stored format human-readable JSON, but you lose automatic undo/redo integration for the array elements.
How to prevent it
- Always set the Custom Class in the model to the exact Swift array type (
[Double],[String], etc.). - Use
@NSManaged(not@objc dynamic) for Transformable properties in SwiftNSManagedObjectsubclasses. - Keep arrays reasonably small; Transformable attributes are serialized as a single blob, so very large arrays can impact fetch performance.
- If you need to query individual elements (e.g., “all statistics where any value > 100”), model the values as a separate to-many relationship instead.
FAQ
Can I store an array of custom structs with Transformable?
Yes, provided the struct conforms to NSSecureCoding (via NSObject subclass) or you implement a custom ValueTransformer. For plain Swift structs, prefer JSON encoding to a Binary Data attribute.
Why do I get an NSArray instead of [Double] at runtime?
The Custom Class field in the data model is either empty or set to NSArray. Update it to [Double] (including brackets) and regenerate the NSManagedObject subclass.
Does Transformable work with SwiftData?
SwiftData uses @Attribute(.transformable) on the property and infers the type from the Swift declaration. The same principle applies: the array is encoded via NSCoding/NSSecureCoding into a Data column.
Parts of this guide are adapted from content licensed under CC BY-SA 3.0; this article is shared under the same license.