Using mock data for previews
So far, we've built apps using our data. However, when dealing with large projects, data is usually obtained by making API calls. But that may be time-consuming and quickly become a bottleneck. The faster option would be to make some mock data available only at build time.
In this recipe, we will store some mock JavaScript Object Notation (JSON) data on insects in our Preview Content
folder and fetch our data from the file instead of making API calls. JSON is lightweight format used for storing and transporting data.
Getting ready
Create a new SwiftUI project called UsingMockDataForPreviews
.
To get access to the files used here, clone/download this project from GitHub: https://github.com/PacktPublishing/SwiftUI-Cookbook-2nd-Edition.
How to do it
We will add a JSON file with sample data to our Xcode project and design an app that elegantly displays that data. The steps are as follows:
- Open the
recipe6
folder for this project...