Work with Core Data in Swift Language

Lets take a brief idea about how to use core data with swift language.

Create a new project by check on Use Core Data.

Core Data Project Setting
Core Data Project Setting

It will create the basic methods of core data in to the AppDelegate.swift

There is core data model called {YOUR-PROJECT.xcdatamodeld} will be there with the project.

It will be look like as follows :

Core Data Model
Core Data Model

I have created one entity called “Devices”. You can see in the above screenshot.

Import the Core Data framework

For the functionalities of core data we have to import the core data framework.

Lets Save the Data in the Entity

We will get the NSManagedObjectContext reference from the AppDelegate.

NSEntityDescription is used for find the Entity.

NSManagedObject will create new object in the Entity.

save() method is used for save the context.

Get all Data from Entity and Fill the table

NSFetchRequest is used to fetch the data from the Entity.

Sample Code

In the next tutorials I will try to add about the Insert, Update and Delete functionality with the use of Core Data.

Happy Coding 🙂

  • can u add query Insert, Update and Delete functionality in Core Data.

  • Muhammad Rizwan Mughal

    can u do make an article with example of core data with relationships? simply in “to many relationship”