CoreData — the easy way

UserList CoreData App

In this article, I’ll explain how to get started with CoreData with SwiftUI. CoreData isn’t as difficult as it was years ago.

Create UserLIst

First, create a new Xcode project called “UserList”, check CoreData and we’ll use SwiftUI. Save the project at the location of your choice.

In this case we use partly the provided Swift files and partly the provided code. First we look at the UserList.xcdatamodeld and change the entries.


We create / change the entity to User with an attribute name which will map the type String. In addition, we select the attribute as above and uncheck Optional. Because the name is always present with User and is not optional.

Next we delete the Persistence.swift file as we are creating our own Swift file. We now create a new Swift file and name it -> CoreDataStack.swift.

Add the code below to the CoreDataStack.swift file.

First we create a class and initialize the data model. Then we have three extensions in which we save, call up or delete the user. The user is then synchronized with the data model.

We have to make another change in the UserListApp.swift file in order to make our CoreDataStack available in SwiftUI.

Here we call the CoreDateStack and link the stack with the ContentView. In ContentView.swift I have the code as follows:

In ContenView we create the design of the app, there are no limits here. We also create a constant for the CoreDataStack and call its functions.

With this article I just wanted to show you how easy it is to integrate CoreData in an app. The code for this project can also be found on Github:



Software ( Swift & 🐍 Python) Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store