Steps to Adding New Entity (Swift Core Data)

 

This assumes an existing .xcdatamodeld file in the project.

 

Step 1: add entity

 

Step 2: add attribute

 

Step 3: set Codegen in data model inspector

Class Definition means the NSManagedObject subclass will be generated for you .i.e. Xcode autogenerates subclass for you.

Manual/None means means you do something yourself which I don’t really understand yet. It was used to create Photo entity in chapter 22 of BNR iOS 7th ed.

Category/Extension option allows you to define an NSManagedObject subclass while still allowing Xcode to generate the extension that defines the attributes and relationships

Source: page 785 BNR

 

Step 4: add relationships to each entity.

Example from BNR

Graphical user interface, text, application

Description automatically generated

 

Inverse relationship?

Text

Description automatically generated

 

Graphical user interface, text, application

Description automatically generated

 

 

Step 5: regenerate …Properties.swift file

In the Photorama application we selected the Photo+CoreDataProperties.swift file and deleted by “Move to Trash”.

Then open Photorama.xcdatamodeld > select Photo entity > Editor > Create NSManagedObject Subclass. With photo checked, click next. Then it asks which entities would you like to manage, since we are only custom managing Photos, just keep Photo checked but nothing else.