How to get an object from SwiftData in Swift

A step by step guide on gathering a specific object from a SwiftData persistent storage container using SwiftUI, UIKit or AppKit.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 09/12/2023 at 12:01
Last Updated on 09/12/2023 at 17:40
<p>Three flowers that represent Swift, Swift Data and Xcode.  The text "Get a SwiftData object" sits beneath them.</p>

A step by step guide on gathering a specific object from a SwiftData persistent storage container using SwiftUI, UIKit or AppKit.

SubscribeCheck out our SwiftData Quick Start Guide

In order to get an object, you must have created a SwiftData Model and persistent container.

Once you have done this, you can create a function that uses a Predicate macro to search the model for an object using a filter.

To learn more about this consult the WWDC videos linked below.

WWDC23: Build an app with SwiftDataWWDC23: Dive deeper into SwiftData
<p>A screenshot of an iPhone showing the result of the sample app available on our open source swift GitHub repository.</p>

The following tutorial has been made available, along with a simple app for demonstrating SwiftData, through the tutorial/swift-data-basics branch on our Open Source Swift Starter Project.

git clone git@github.com:delasign/swift-starter-project.git
View RepositoryHow to create a SwiftLint enabled Swift XCode Project

Step One: Create the Model

<p>A screenshot of Xcode showing a completed model in the SampleSwiftDataModel.swift file.</p>

In the project, create a SwiftData model for the objects.

We recommend that you use a unique attribute, to allow you to efficiently search for specifically objects.

How to create a SwiftData Model in Swift

Step Two: Create the Persistent Container

<p>A screenshot of Xcode showing the DataCoordinator.swift file. Highlighted on the file is the code used for the persistent container within a main actor, and theres an empty array that will hold all the objects once we have fetched them. The sample code is below.</p>

Create a persistent container and an array to hold the objects using code similar to that below.

Please remember to replace _SwiftDataModelName_ with the name of the model that you created in Step One.

How to create or access a SwiftData Persistent Container in Swift

We recommend placing this functionality within a Singleton that acts as a MainActor. If you do not use a MainActor, you will have to use Async/Await patterns.

How to create a singleton in SwiftHow to use async and await in Swift projects

Step Three: Add the Functionality

<p>A screenshot of Xcode highlighting the functionality that is available in a code snippet below that allows you to get an object by id.</p>

Add functionality similar that below to the project to fetch a single objects based on the model you created in Step One.

Please note that we are filtering by id, as this is the unique attribute that we declared in Step One.

We recommend adding this to a file called DataCoordinator+SwiftData.swift, where DataCoordinator is the name of the MainActor singleton that manages the SwiftData.

For more information on FetchDescriptors and Predicates, we recommend checking out the links below.

Read Apple's Documentation on the FetchDescriptorRead Apple's Documentation on the Predicate StructureWWDC23: Build an app with SwiftDataWWDC23: Dive Deeper into SwiftData

Looking to learn more about things you can do with SwiftData, Swift or XCode ?

Consult our quick start guide or search our blog to find educational content on learning how to use SwiftData, Swift or XCode.

Consult our SwiftData Quick Start GuideSearch our Blog

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
SubscribeContact UsVisit our BlogView our ServicesView our Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe