Reader small image

You're reading from  iOS 17 Programming for Beginners - Eighth Edition

Product typeBook
Published inOct 2023
Reading LevelBeginner
PublisherPackt
ISBN-139781837630561
Edition8th Edition
Languages
Tools
Right arrow
Author (1)
Ahmad Sahar
Ahmad Sahar
author image
Ahmad Sahar

Ahmad Sahar is a trainer, presenter, and consultant at Tomafuwi Productions, specializing in conducting training courses for macOS and iOS, macOS Support Essentials certification courses, and iOS Development courses. He is a member of the DevCon iOS and MyCocoaHeads online communities in Malaysia and has conducted presentations and talks for both groups. In his spare time, he likes building and programming LEGO Mindstorms robots.
Read more about Ahmad Sahar

Right arrow

Getting Started with JSON Files

In the previous chapter, you modified the Add Journal Entry screen so that the user can add their current location to a new journal entry, and configured the Map screen to display a region centered on your current location as well as pins representing the location where the journal entries are made. However, since the JournalListViewController class and the MapViewController class are using separate instances of the SampleJournalEntryData structure, newly added journal entries do not appear on the Map screen as pins. Also, all the journal entries are lost when you quit the app.

In this chapter, you will create a singleton, SharedData, that will provide journal entry data to both the Journal List and Map screens. This class will also be used to load journal entry data from a file on your device when the app starts up and save journal entry data to a file on your device when you add or delete journal entries.

You’ll start by creating the...

Technical requirements

You will continue working on the JRNL project that you modified in the previous chapter.

The resource files and completed Xcode project for this chapter are in the Chapter18 folder of the code bundle for this book, which can be downloaded here:

https://github.com/PacktPublishing/iOS-17-Programming-for-Beginners-Eighth-Edition

Check out the following video to see the code in action:

https://youtu.be/1f1ZTjRHWSA

Let’s start by creating a new singleton to store the data used by your app.

Creating a singleton

At present, when you add new journal entries to your app, they will appear on the Journal List screen, but when you switch to the Map screen, the newly added journal entries are not present. This is because the JournalListViewController class and the MapViewController class are using two separate and unrelated instances of the SampleJournalEntryData structure. To solve this issue, you’ll create a new singleton to store your app data. A singleton is created once and then referenced throughout your app. This means that the JournalListViewController class and the MapViewController class will be getting their data from a single source.

You will create a singleton named SharedData and configure the JournalListViewController and MapViewController classes to use it. Follow these steps:

  1. In the Project...

Modifying the JournalEntry class to be JSON-compatible

At present, all app data is lost when you quit the app and you will need to implement a way to save your app data. iOS provides many ways to store your app data. One of them is converting the data to JavaScript Object Notation (JSON) format, and then writing it as a file to your device storage. JSON is a way to structure data in a file that can be easily read by both people and computers.

To help you to understand the JSON format, look at the sample shown below:

[
  {
    "dateString": "May 17, 2023"
    "rating": 5
    "entryTitle": "Good"
    "entryBody": "Today is a good day"
    "photoData": "<photo data for the sun.max image>"
    "latitude": 
    "longitude": 
  },
  {
    "dateString": "May 17, 2023"
    "rating": 0
    "entryTitle": "Bad"
    "...

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.

A JSONEncoder instance encodes instances of a data type to JSON objects, and you will use it when saving files to your device storage.

Summary

In this chapter, you created a singleton, SharedData, and configured your app to use it in place of the two separate SampleJournalEntryData structures. Next, you modified the JournalEntry class to be compatible with the JSON format, so you can save journal entries to a JSON file and load journal entries from a JSON file. After that, you added methods to save journal entry data when you add or delete journal entries, and to load journal entry data when your app is starting up.

You now know how to create a class to store, load, and save data from JSON files for use in your own apps.

In the next chapter, you’ll implement a custom user interface element that allows you to set star ratings for journal entries.

Learn more on Discord

To join the Discord community for this book – where you can share feedback, ask questions to the author, and learn about new releases – follow the QR code below:

https://packt.link/8hBmp

lock icon
The rest of the chapter is locked
You have been reading a chapter from
iOS 17 Programming for Beginners - Eighth Edition
Published in: Oct 2023Publisher: PacktISBN-13: 9781837630561
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Ahmad Sahar

Ahmad Sahar is a trainer, presenter, and consultant at Tomafuwi Productions, specializing in conducting training courses for macOS and iOS, macOS Support Essentials certification courses, and iOS Development courses. He is a member of the DevCon iOS and MyCocoaHeads online communities in Malaysia and has conducted presentations and talks for both groups. In his spare time, he likes building and programming LEGO Mindstorms robots.
Read more about Ahmad Sahar