Real World Example: Creating a Journal Entry App with Core Data in Swift Part 1.

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.
  • 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.

Thinking back I should've called it entry . Though you can call it whatever you want, doesn’t really matter.

Done with that.

On with writing code

We need to save the text that the user enters in the UITextView.

Open the file for your ViewController file, I called mine addItemViewController.

Saving data

Before you do this, make sure your UI objects are connected.

To save the data typed into the UITextView, write this into your button’s action block.

This may seem a bit long and complicated at first but it’s quite simple.

It’s a basic if else statement.

The first part is checking if the itemEntryTextView(UITextView) is empty or if it’s equal to Type anything… and if so, then alert the user. Reason being I wanted some type of placeholder. UITextViews don’t have placeholders so I gave it some default text and then put it to some light grey color in storyboard.

Later I’ll show you how to change the text color to black once a user starts typing.

The second part is accessing the properties in the AppDelegate and then using it to save the data. If you take a look in your AppDelegate, there’s some code there by default that Xcode provides when we checked yes to using Core Data.

Then last line is to dismiss the view.

Fetching Data

We will be displaying the saved data in our UITableViewController. I named mine DisplayTableViewController.

First let’s get access to the properties in AppDelegate.

We also will be storing the fetched data into an array.

Create a function that will be called to grab the data.

Instead of calling this function in the viewDidLoad, we can call this in the viewWillAppear so that every time the view is shown, the data will be up to date. This is important when we dismiss our addItemViewController .

Setting up the UITableView

If you’ve ever set up a UITableView before you know it’s pretty straightforward.

Add the following methods:

I put it in an extension just to make the code for the UITableView separate.

In the cellForRow do the following:

We’re basically grabbing each value from the array and setting it as the cell’s text.

In numberOfRows we need to return the count of the array.

Run this in simulator and we should now have a working application where we can save data and display it in a table.

Make sure you toggle have Connect Hardware Keyboard checked in the simulator so that you can access the button at the bottom of the screen.

In menu bar:

Hardware -> Keyboard -> Connect Hardware Keyboard
In Part 2 I will show you how to dismiss the keyboard upon pressing return on the keyboard.

More work to be done.

In part 2, I’ll be going over these issue’s that our app current:

  • When we don’t like an entry, delete the data with slide to delete.
  • Hide keyboard when done typing
  • Have dynamic cell heights, so no text would be cut off.
  • Make the newest task at the top.
  • We’ll be adding the date & time stamp to each post.
  • For fun I’ll also show you how to use custom colors for buttons etc using RGB values.

Look forward for you to read part 2!

Thank you as always!