Deleting Core Data objects from a SwiftUI view
How can you delete objects from a Core Data repository? Removing objects is almost as important as adding them. In this recipe, we'll learn how to integrate the Core Data delete options into a SwiftUI app.
Getting ready
Let's create a SwiftUI app called DeleteContactsApp
, ensuring that we check the Use Core Data checkbox, as shown in the following screenshot:
How to do it…
We are going to reuse part of the code provided in the Showing Core Data objects with @FetchRequest recipe. Please refer to that recipe if you want to find out more.
Let's get started:
- The first step is to create the Core Data model. To do this, select the
DeleteContactsApp.xcdatamodeld
file and do the following:a. Add a new entity called
Contact
(1)b. Add three string attributes called
firstName
,lastName
, andphoneNumber
(2)c. Check that Codegen...