How to use Core-Data with SWIFT

Core data is a framework that is used to save data in iOS app development and OS X platform. Today we’ll develop an application using SWIFT programming language that will have a textfield, a button and a textview that will save and display data fetched from Core Data. To learn more about Core-Data please visit:

Introduction to Core Data Programming Guide


Step 1: Create Project

Step 2: Design Core-Data database

Step 3: Write script to save and fetch data from core data.


Step 1:

1. Create a new project in Xcode. In first window of the wizard select Single view application. In next screen, select SWIFT from dropdown box and also check “Use Core Data” checkbox (bottom of the wizard). Please use “NotePad” as your project name.

Screen Shot 2015-02-12 at 11.56.09 PM

2. Open Main.storyboard and add a textfield, a button. Below the button add big textview. ( Note: textfield is for one line. For multiple lines use textview).

Screen Shot 2015-02-13 at 12.06.29 AM


Step 2:

1. Now, open NotePad.xcdatamodeld (YourProjectName.xcdatamodeld). This is the place where you design your database.

2. Here you add Entity (Entity is similar to Table of Oracle / MySql database, but not exactly). At the bottom of the page, you will see Add Entity button (1). Click on it. New entity (2) will be added. By double clicking on it, you can change it’s name. I changed it to “Note”

3. Now select the entity (if it’s not already selected). Add “Attribute” to it by clicking + button (3) below the Attributes section. Attribute is similar to column of a table. For each attribute we should select a type from dropdown box. I’m using only one attribute “paper” with type “String”.

4. At this point you have to do an important thing. In Utilities panel (Right side bar), you will see a box labeled: “Class” (4) in entity section. Write here : NotePad.Note; Actually, it should be ApplicationName.EntityName

5. Now we’ll generate Entity Classes. To do this, go to Editor > Create NSManagedObjectSubclass. Select the data model (It’s the file name that we have worked on). In next screen, select Entity name. We’ve only one Entity. Click next. Now, you will see a dialog box for saving entity file. Here, from language dropdown box select swift and finally click Create button.


Step 3:

1. Add outlets to textField and textview.

2. Add IBAction to button. You can also rename the button to “Add”

3. Open ViewController.swift.

We’ll edit this file. Before we proceed, let me give you an example. Suppose we have some food, one tray and a refrigerator. To save the food, we’ve to put it on the tray and keep the tray inside refrigerator. Saving method of Core data is similar. Here data is food, managedObject is tray and is equivalent to keeping tray contained food inside refrigerator.

Screen Shot 2015-02-16 at 9.45.25 PM


First import CoreData in your viewController

declare an array NSManagedObject below IBOutlet declaration. so the code will look like this:


To fetch data from CoreData we can use following method:

Above method fetches all data in Note entity. At the end of the method, we used for loop to get each text inside paper and add them in a local variable. finally show all texts in large text view.

To save data in Core data use following code. We are saving text in paper attribute (column) of Note entity (table)


Now implement saveNote in IBAction actionAddNote. Clear the text field so that you can add new note.


Full source code of ViewController.swift. You can also download the whole project from the link provided at the end this tutorial.


You can download the whole project by clicking here.

1 thought on “How to use Core-Data with SWIFT”

  1. Hello–I am very interested in your tutorial, but when I try to do this in Xcode 7, I get several errors.

    In your ViewController.swift, I get an error on line 49 (line 41 above) and on line 111 (line 103 above).

    41 let fetchResults = managedObjectContext.executeFetchRequest(fetchRequest, error: &error) as [NSManagedObject]?
    103 if

    The project won’t build. Can you please update the code to work with the most recent Xcode? Or at least let us know how to make the corrections to the code?

    Thank you!

Leave a Reply