Core Data with Swift in iOS App Development

Core Data is a framework which you can use to manage Data or Model

To work with following code, you need to create single view application.
Please select the core data check box when you create the project

Then you have the coredataex.xcdatamodeld to create the Entity and its attribute

Here I have created the User Entity and two attributes named firstname and lastname

How to Insert Data
You can write the following code inside the viewDidLoad() method

When you run the application it will insert the data into the database.

How to Retrieve Data
If you want to get data from the User entity you can use the following code

How to delete entry
If you want to delete a row you have to select that row first. I am using NSPredicate class to select the recods where firstname is equal to “Mark”
Once you select the row using context.fetch() you can use context.delete() to delete the record from the entity

Leave a Reply

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