Using mock data for previews
So far, we've built apps using our own data. However, when building an app for a client, you may want to use mock data that closely resembles client data. Using API calls early on in development may be a bottleneck. In this recipe, we will introduce a faster option, using JSON data stored in a file.
In this recipe, we will read Insect
data from a JSON file and display the content in a SwiftUI view.
Getting ready
Let's create a new project called UsingMockDataForPreviews
.
If not yet done, clone this book's GitHub repository from https://github.com/PacktPublishing/SwiftUI-Cookbook.
How to do it…
We will add our mock data to the Xcode project, then proceed to design a SwiftUI view that elegantly displays the data. The steps are as follows:
- From the cloned GitHub repository, open the
Resources
file, then drag and drop theinsectData.json
file into thePreview Content
folder of the Xcode project: