iOS Tutorials: Saving Notes using Core Data

OVERVIEW

Core Data is a framework that allow users to store and retrieve data from it’s devices without having to retype the data every time the user uses an app. However, Core Data shouldn’t be mistaken as a database, as it is not a database.

LET’S DIVE INTO CORE DATA

Create your project and remember that this time, you’ll have to enable Core Data.

By enabling Core Data, you’ll notice an additional file on your left. First, let’s click Add Entity and create some of the attributes shown on the image such as title, details and date. Here, we are going to create a note app that allows you to input the title and details along with the date.

Let’s create our layout first by heading to Main.storyboard. These are the objects required from the Object Library. Make sure you create the required Swift file. You could do that by clicking

File > New > File > Cocoa Touch Class >

(Do it twice by creating a UITableViewController & UITableViewCell) 

The subclass should look like below in order to create the class.

(Subclass of: UITableViewCell) 

(Subclass of: UITableViewController)

Let’s get into the TableViewCell that you’ve just created and link the object together. Create an IBOutlet for each of the required object. If you don’t know how to do that, please check out my other tutorial on how to create an IBOutlet. Also, make sure to tick on Is initial View Controller on the right as we want this screen to appear first.

Lets write a function here that takes in the data from the Entity that we created earlier named Note. With this function, the data will appear on the TableView.

Let’s click on the TableViewController and edit some of it’s content. Connect the layout with the class by inputting whatever name you use for the TableViewController. In my case, I use MainTableViewController.

Then create a segue by pressing Control + Drag to the other screen and click show to create a segue. Name that segue editOrAdd.

Now let’s go to the TableViewController.swift that was created few moments ago. I named mine MainTableViewController.swift. We will be  doing some coding here.

In this part of code, we will achieve:

  1. Load the notes that are saved.
  2. Enable the functionality of edit & delete on the tableview.
  3. Enable the functionality of adding a note
  4. Distribute the data to the respective area.
Let’s proceed to ViewController.swift and do some necessary changes. Create an IBAction for the following button.

Before we forget, go to AppDelegate.swift and add the following code below all the code:

The code above allows you to access the delegate application and save the information into the database.

The code is uploaded to GitHub

  • Article By :
    Founder of DaddyCoding. Studied Computer Science, Information System and Information Technology at BYU-Hawaii. Currently spending most of my time researching and learning on helping to expose making iOS apps.

Random Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*