In this article we will be using the basic foundations of Core Data and how to use it in a real world iOS example by creating a simple journal entry application that utilizes Core Data to save , retrieve and delete your entries.

What is Core Data?

Core Data is a framework that you use to manage the model layer objects in your application. It provides generalized and automated solutions to common tasks associated with object life cycle and object graph management, including persistence. — Apple Documentation

It’s a framework just like UIKit provides UIButtons , UILabels , UIViews Core Data provides a way to manage data/models.

With Core Data you can save, retrieve, and delete data from disk.

Core Data is not a Database

Terminology

Let me define you some terms so when we get to them you won’t be so confused.

We will be encountering these terms:

Entities — Think of this as people.

— Think of this as people. Attributes — Think of this as all the information about people. Like name, email, age, birthday, location etc.

Before we start.

In your Xcode project, you should see a file on the left hand side that is named like so: yourAppName.xcdatamodelId

If you don’t see this, you didn’t check the box “Use Core Data”.

If you want to add this to an existing Xcode project do the following:

File -> New -> File

Under iOS -> Under Core Data -> Data Model

You’ll also need to make sure to appropriately make changes to your AppDelegate.swift. (Take a look at mine at my GitHub)

Our Journal App

We’re going to end up creating something like this, where we’ll be able to add new entries to our list and delete any entries we don’t want anymore.

User Interface

For this example I quickly threw this up in storyboard.

I won’t be going over how to implement the user interface and all the IBOutlets. As always though I will have the full project on my GitHub. Feel free to use it as a reference or even download it and screw around with the files to see what else you can add to it.

Wondering what that .xcdatamodelId is?

Open the file ending in .xcdatamodelId .

This is where we can create Entities , and add Attributes to them.

On the bottom left click on Add Entity .

Then name the Entity (click once on the text) whatever you’d like. I named mine Item .

Now click on that Entity , and on the bottom right click Add Attribute .

Give the attribute a name, I simply named mine name with type String .