Transformable types

Andreea Andro
3 min readJan 28, 2023

Saving a custom type with Core Data.

Let’s say we need to persistently save the info from bellow, representing the speeds we can work with in our app.

"motor_configuration": {
"max_speed_limit": {
"20mph": [100, 101],
"25kph": [200, 201],
"28mph": [300, 301],
"25kphTW": [150],
"25kphJP": [160]
}
}

It’s a dictionary of max speed limits.
- the key is a human readable speed written as a String
- the value is a list of equivalent speeds expressed as Integers in a certain unit of measure
We don’t know how many elements will be in the dictionary or in the arrays.

How would you save this data?

I’ll show how to save it in CoreData as a transformable type.

  1. Create a new Entity, let’s name it CustomAppSetting, with an attribute motorConfiguration, of type Transformable. When using the Transformable type, we have to provide values for
    - “Custom Class”, I’ll call it MotorConfiguration (and I’ll define it later)
    - “Transformer”, I’ll call it MotorConfigurationTransformer (and I’ll define it later)
How to define the attribute

2. Define the custom classes

public class MotorConfiguration: NSObject {
let maxSpeedLimit: [String: [Int]]…

--

--

Responses (1)