Now It’s time to take our apps to a next-level and equip them with a long-term memory!
For that, CoreData framework will come to our assistance.
Core Data Overview
CoreData provides functionality to manage the Data Model of an application, including persistence. Simply put, CoreData is an object-graph-manager.
What's an object graph?
Let’s make an example of an object-graph.
Say, we have a Person object. It can be described by a number of properties:
First name
Last name
Date-of-birth.
A person may be associated with an address. For that we would have an Address object and connect it to a Person object. An Address object, in its term, can be described with a few properties as well:
Street number,
Street,
City,
Province,
Country.
Evidently a person can be associated with multiple addresses, like home or office address. And each address can be associated with multiple people. If we establish all these connections, we’’ll get an object-graph:

For instance, John Smith lives in New York on 5th Avenue and works on Wall street. And Livia Anderson works on Wall Street in the same building but lives on 42nd Street. Connecting these objects gives us an object-graph describing 2 people:

Let's establish some terminology in CoreData context:
Objects, like Person or Address are called Entities.
Objects' properties - First and Last names or Country and Province are called Attributes.
Connections between objects are called Relationships.

"Amazing!" in the making
Let’s explore the essentials of Core Data functionality experimenting with a funky mood-tracking app called simply "Amazing!".
The app will allow the user to log their mood any day, any number of times in a day, however, only the latest ‘mood’ log will be remembered. We’ll also have an option to snooze for the day in case a user explicitly wants to erase tracking for a current day. We’ll then provide observing tools for the user to analyze their mood over time.
Here's our initial interface:

Go ahead and start the project by creating a Single View application in Xcode.
Arrange the elements using the assets below (use buttons for each option, two labels for each text line and have them connected as outlets and actions. And of course, don't be lazy - add constraints ):
![]() | ![]() | ![]() | ![]() |
Or, switch on a cheat-track and download the starting project.
If you decided to implement your own version, here's how your view controller file should look like:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var moodLabel: UILabel!
@IBOutlet weak var happyButton: UIButton!
@IBOutlet weak var sadButton: UIButton!
@IBOutlet weak var neutralButton: UIButton!
@IBOutlet weak var snoozeButton: UIButton!
@IBAction func moodButtonPress(_ sender: Any) {
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
As you may notice, there's only one method for button press. It's linked to all of the buttons:

One last thing for the interface configuration - to prepare for the future, we'll need to identify which button the user pressed. We could compare sender object to each of the button variables we have in code. However, we're going to use a different approach. We'll utilize a 'tag' property of a view - it's an integer value that can be used for any suitable service.
Go ahead and assign a number 0-3 to the 4 buttons we have:

The numbers should be as follows:
0 - Happy
1 - Neutral
2 - Sad
3 - Snooze
Creating Data Model
A new file we are going to work with here - Amazing.xcdatamodeld
. This is where we create our entities, attributes and connections. Our starting app example will require a single entity - Mood. We'll describe it by two attributes:
Emotion
Date
Go ahead and create a new entity:

Rename the newly created entity to 'Mood' by double clicking on the default name and typing in a new one:

And then, add the intended attributes by clicking +
button at the bottom of Attributes section:

Let's orient ourselves in Xcode once again, this time in the context of data model:

Connecting to code
There are two important names that entity carries - Entity name and Class name, they both can be configured on Data Model Inspector:

Entity name is used when passing requests to objects of Core Data framework. And Class name is how a managed object of an entity becomes available in the code.
The availability in the code does not happen automatically, this is set by configuring 'Codegen' attribute of an entity. Setting it to 'Class Definition' makes it automatically defined as a class in our project that we'll be able to utilize.
You are welcome to try different names and try to refer to it in the code.
Preventative troubleshooting
If you are trying to switch the names back an forth and getting an error when everything seems to be correct. Try to run a Clean operation in the Xcode in the main menu select Product->Clean (or, Shift + Command + K).
A final touch to the entity configuration - it's logical to request that our logs have both values present - required attributes. To make the attributes required, select each attribute and uncheck its 'Optional' property:

All set, in the next chapter we'll prepare to manipulate the data!
Let's Recap!
CoreData is a framework that provides functionality to manageData Model of an application. It is an object-graph manager.
Object-graph includes main components - Entities, Attributes,Relationships.
CodeData Data Model is described in a file with extension .xcdatamodeld that allows to create and configure data components.
Data model components are connected to the code using Entity name and Class name.