The data model is a key component of the Core Data stack and an integral part of every Core Data application. In this tutorial, we explore the data model and learn about entities and attributes.

Setting Up the Project

The best way to learn is by doing. Open Xcode and create a new project, choosing the Single View Application template. Name the project Data Model and tell Xcode to populate the project with boilerplate code for Core Data by checking the Use Core Data checkbox.

Data Model, Entities, and Attributes | Setting Up the Project

Data Model, Entities, and Attributes | Setting Up the Project

Take a quick look at AppDelegate.swift to refresh your memory. The application delegate creates an instance of the NSPersistentContainer class, which is responsible for setting up and managing the Core Data stack.

Exploring the Data Model

When the application sets up the Core Data stack, the managed object model loads the data model from the application bundle. The file that's loaded from the application bundle is named Data_Model and has a .momd extension. The extension stands for managed object model document.

If you open the Project Navigator on the left, you should see a file named Data_Model. The file's extension, .xcdatamodeld, doesn't match, though. And yet if you run the application, the Core Data stack is set up without issues. How is that possible?

When the application is built, the data model file you see in the Project Navigator is compiled into a .momd file. Why is that necessary? Why does the data model need to be compiled?

The .xcdatamodeld file is the file we edit in Xcode during development. We use it for defining the application's data model. It shows us a visual representation of the data model as we will see in a moment. Much of the information in the .xcdatamodeld file is not needed for Core Data to do its work. At compile time, Xcode collects the data it needs from the .xcdatamodeld file and creates a .momd file. It is the .momd file that is included in the application bundle. The resulting .momd file is much smaller, containing only what is absolutely essential for Core Data to infer the data model.

Exploring the Data Model Editor

To edit the data model, Xcode ships with a powerful data model editor. In the Project Navigator on the left, select Data_Model.xcdatamodeld. The data model editor should automatically open, showing you the data model.

Styles

The data model editor has two styles, table and graph. You can toggle between these styles with the control in the lower right of the data model editor.

Exploring the Data Model Editor | Toggling the Editor Style of the Data Model Editor

The table style is useful for adding and editing entities, attributes, and relationships. Most of your time is spent in the table style. The graph style is ideal for visualizing relationships between entities. But what are entities?

Exploring the Data Model Editor | The Graph Style of the Data Model Editor

Entities and Attributes

Even though Core Data isn't a database, you can think of an entity as a table in a database. Entities have a name and properties. A property is either an attribute or a relationship. For example, an entity named Person can have an attribute firstName and lastName. It could also have a relationship address that points to an Address entity. We discuss relationships in more detail later.

You can add an entity by clicking the Add Entity button at the bottom of the data model editor. The editor's table style is split into a navigator on the left and a detail view on the right. In the Entities section of the navigator, you should see an entity named Entity.

Select the entity, open the Utilities pane on the right, and select the Data Model Inspector. The inspector shows the details of the entity. Set the name of the entity to Recipe.

Data Model, Entities, and Attributes | Edit the Name of an Entity

With the Recipe entity selected, click the Add Attribute button at the bottom to add an attribute to the entity. In the Attributes table, set Attribute to name and Type to String. You can inspect the details of an attribute by selecting it and opening the Data Model Inspector in the Utilities pane on the right.

Data Model, Entities, and Attributes | Add an Attribute to an Entity

In the Data Model Inspector, you can see that an attribute has a number of configuration options, including validation rules, a default value, and indexing options. Some of the options differ from type to type.

Before we move on, I would like to point out that the checkbox Optional is checked by default. This indicates that the attribute name is optional for the Recipe entity. What does that mean?

If we create a recipe and the value of the name attribute is not set, Core Data won't complain because the attribute is marked as optional. If you make the attribute required by unchecking the checkbox, Core Data throws an error if you try to save a recipe that doesn't have a name.

Questions? Leave them in the comments below or reach out to me on Twitter. You can download the source files of this tutorial from GitHub.

Now that you know what Core Data is and how the Core Data stack is set up, it's time to write some code. If you're serious about Core Data, check out Mastering Core Data With Swift. We build an application that is powered by Core Data and you learn everything you need to know to use Core Data in your own projects.