Member-only story
Subclassing NSManagedObject for Core Data
Key terms, rationale behind subclassing, data types, automatic subclass creation, and storing images and NSCoding as transformable in Core Data
5 min readJul 13, 2023
Terminology
- Core Data: A framework that allows you to manage the model layer objects in your application. Core Data does this by being an object graph management and persistence framework.
- Attribute: The data that will be stored in a Core Data entity. The data type begins as Undefined, and can then be changed
- @NSManaged: Indicates that extra code will be automatically provided when the code runs; Core Data takes care of the implementation that will be generated at runtime
Why subclass NSManagedObject?
There are many Core Data data types to get to grips with. By subclassing NSManagedObject it is possible to make your own classes for each data entity, creating a on-to-one mapping between entities and classes defined in code.
Once the NSManagedObject is subclassed you can modify the generated class to include custom:
- Constructors
- Data Conversion Methods
- Validation
- Formatting