Entities

Module 1 of 11 0%

Entities

4. Creating our first Entity

In the overview chapter, we took a look at the TaskItem structure:

/// A representation of a task.
struct TaskItem {

	/// The title of the task.
	let title: String

	/// An optional subtitle associated with the task.
	let subtitle: String?

	/// Specifies whether the task is completed or not.
	let isCompleted: Bool
}

In order to persist this data, we must create a Core Data entity with the same properties. Start by tapping the Add Entity button at the bottom of the screen:

Group 25.png

This will create a new entity, which will be visible on the left side of the model editor. I like to differentiate between TaskItem and my Core Data entity by adding a “Stored” prefix - StoredTaskItem.

Group 26.png

With our entity created, let’s add the necessary attributes.

Group 27.png

We created the same attributes as our structure and assigned to appropriate type:

Finally, since we know that title and isCompleted are both required properties, we should uncheck the Optional property from both:

Group 28.png

With this in mind, our basic entity structure is done. Our next step is to initialize Core Data and use our newly created entity in code.