Loading and saving JSON data
Now that you have modified the JournalEntry
class to conform to the Codable
protocol, you are ready to implement loading data from and saving data to JSON files.
There are many ways to load and save app data. In Chapter 23, Getting Started with SwiftData, you’ll learn how to use SwiftData to load and save app data.
To make it easier for you to work with JSON files, Apple provides JSONDecoder
and JSONEncoder
classes.
A JSONDecoder
instance decodes instances of a data type from JSON objects, and you will use it when loading files from your device storage.
To learn more about JSONDecoder
, see https://developer.apple.com/documentation/foundation/jsondecoder.
A JSONEncoder
instance encodes instances of a data type to JSON objects, and you will use it when saving files to your device storage.
To learn more about JSONEncoder
, see https://developer.apple.com/documentation/foundation/jsonencoder.