Adding Core Data to an existing project in Xcode 10 & Swift 4

Add Core Data functionality to your existing project in just a few steps.

Core Data is a framework provided by Apple for managing the model layer in our applications. When talking about MVC (Model, View, Controller) architecture, Core Data is essentially the Model part.

If you’re developing an iOS app in Xcode and you reach a point where you decide you need to use Core Data but it’s not enabled in your project — fear not! It’s possible to add in a few simple steps, which we’ll go through in this article. This technique will also allow you to easily add Core Data to other app project types such as Page Based or Tabbed Apps.

We will be carrying out the following steps to get Core Data into our existing app:

  • Create a new Single View App with Core Data enabled — we can delete this project when we’re finished
  • Copy the relevant parts from this project’s AppDelegate.swift into our existing project’s AppDelegate.swift
  • Create a new Core Data Data Model in our existing project and name it appropriately
  • Ensure the Data Model is referenced correctly
  • Test!

So, with your existing project open, create a new project in Xcode (⇧⌘N) and select a Single View App, you can call it whatever you like as we’ll be deleting it when we’re done. You’ll see the “Use Core Data” checkbox on the project options screen, make sure it is checked.

Now we need to open the AppDelegate.swift file in this new project and scroll down toward the end of the file, where we need to copy everything from applicationWillTerminate() to the end of the file:

Open your existing application and paste all of the above at the end of your application’s AppDelegate.swift file.

We’re now done with this temporary project, so feel free to close it and delete it.

At the top of your updated AppDelegate.swift file, you’ll need to import the Core Data library, so after this line:

Add:

Now we need the actual Data Model file, so press ⌘N, scroll down to the Core Data section and select Data Model.

Choose a name (it makes sense to name it the same as your project), then you should see the yourProjectName.xcdatamodeld in the Xcode file manager.

The final step is to go back to the AppDelegate.swift and make sure that the following line:

Is updated to reflect the name of the Data Model file you have created (i.e replace “your-data-model-file” with the yourProjectName part of yourProjectName.xcdatamodeld.

You can now go into the Data Model and start adding Entities and their Attributes, or whatever you need from the Core Data functionality.

That’s pretty much it! We hope this article has been useful and please get it touch if we can help with any of your app projects or ideas.

Like this story? Please follow us on Twitter.

At Welcm we design, develop and support touch screen applications and systems.

If you have a project you would like to discuss please send an enquiry from our contact page, email us at enquiries@welcm.uk or call us on 01252 950 650.

We also make Visitor Management Easy at https://welcm.ly

Originally published at welcm.uk.