How to add Core Data to an existing iOS project

Guru
1 min readMay 15, 2021

Recently I started working on an iOS app, and one fourth of the way through, I realised I needed Core Data, and I hadn’t ticked the box that enabled Core Data while creating the project. Here’s what I did:

1. Create a new code project, give it any name you want.

2. In the AppDelegate file, copy all the code block from // MARK: — Core Data stack including the // MARK: — Core Data Saving support

2. In the AppDelegate file, copy all the code block from // MARK: - Core Data stack including the // MARK: - Core Data Saving support

3.Paste it in the App Delegate file of your current project. Make sure its within the class {} not outside it.

5. Change the project from the dummy project name you created to the current project name where you have pasted the code. You will find it under the lazy var persistentContainer: NSPersistentContainer = {

let container = NSPersistentContainer(name: “Your “current project nee here)

…}

6. Create a New File, from the Core Data tab, select Data Model option, click Next till the file is in the project.

7. Build and run just to make sure it works.

Hope that helps.

--

--

Guru

Author| Senior freelance writer| Novice iOS and Android Developer| Interests: Classical guitar enthusiast|Pottery | https://about.me/gurun