Chapters

Hide chapters

iOS Apprentice

Eighth Edition · iOS 13 · Swift 5.2 · Xcode 11

Before You Begin

Section 0: 3 chapters
Show chapters Hide chapters

Checklists

Section 2: 12 chapters
Show chapters Hide chapters

My Locations

Section 3: 11 chapters
Show chapters Hide chapters

Store Search

Section 4: 12 chapters
Show chapters Hide chapters

15. Saving & Loading
Written by Eli Ganim

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

You now have full to-do item management functionality working for Checklists — you can add items, edit them, and even delete them. However, any new to-do items that you add to the list cease to exist when you terminate the app (by pressing the Stop button in Xcode, for example). And when you delete items from the list, they keep reappearing after a new launch. That’s not how a real app should behave!

So, it’s time to consdider data persistence — or, to put it simply, saving and loading items…

In this chapter you wil cover the following:

  • The need for data persistence: A quick look at why you need data persistence.
  • The documents folder: Determine where in the file system you can place the file that will store the to-do list items.
  • Save checklist items: Save the to-do items to a file whenever the user makes a change such as: add a new item, toggle a checkmark, delete an item, etc.
  • Load the file: Load the to-do items from the saved file when the app starts up again after termination.

The need for data persistence

Thanks to the multitasking nature of iOS, an app stays in memory when you close it and go back to the home screen or switch to another app. The app goes into a suspended state where it does absolutely nothing and yet, still hangs on to its data.

During normal usage, users will never truly terminate an app, just suspend it. However, the app can still be terminated when iOS runs out of available working memory, as iOS will terminate any suspended apps in order to free up memory when necessary. And if they really want to, users can kill apps by hand or restart/reset their entire device.

Just keeping the list of items in memory is not good enough because there is no guarantee that the app will remain in memory forever, whether active or suspended.

Instead, you will need to persist this data in a file on the device’s long-term flash storage. This is no different than saving a file from your word processor on your desktop computer, except that iOS apps should take care of this automatically.

The user shouldn’t have to press a Save button just to make sure unsaved data is safely placed in long-term storage.

Apps need to persist data just in case the app is terminated
Apps need to persist data just in case the app is terminated

So let’s get crackin’ on that data persistence functionality!

The documents folder

iOS apps live in a sheltered environment known as the sandbox. Each app has its own folder for storing files but cannot access the directories or files belonging to any other app.

Getting the save file path

Let’s look at how this works in code.

Wayhemi aachep gmimecp Cicorawbd heynev uxb risi cozo bofuqoijx
Toxjose oomwiy dhamolg Jijukimgf wupqum etm yubo busa vegebauyt

Browsing the documents folder

For the rest of this app, run the app on the Simulator instead of a device. That makes it easier to look at the files you’ll be writing into the Documents folder. Because the Simulator stores the app’s files in a regular folder on your Mac, you can easily examine them using Finder.

Wva ipz’z vexotfifv ctjimgepa is xca Fipogahog
Zgu inf’j modavkeww xmmorlefu eq wfa Jivoriyes

Ciepicq nse Faveriqyh zoqpaj ecmi oh lwe yocaqo
Noahezp tmu Kojesunfw tugcer edha in qje wofiba

Saving checklist items

In this section you are going to write code that saves the list of to-do items to a file named Checklists.plist when the user adds a new item or edits an existing item. Once you are able to save the items, you’ll add code to load this list again when the app starts up.

.plist files

So what is a .plist file?

Lhu nditutx or sruakogk (tutury) eqq uhnsuosadw (suiqups) oxxaxhz
Spe pjeqofm eh xkiopofs (danowf) egw usqcoucixk (xaafigf) ezmungf

Saving data to a file

➤ Add the following method to ChecklistViewController.swift:

The Codable protocol

Swift arrays — as well as most other standard Swift objects and structures — conform to the Codable protocol. However, in the case of array, the objects contained in the array should also support Codable if you want to serialize the array. So is our ChecklistItem class Codable compliant? Probably not.

Using the new method

You have to call the new saveChecklistItems() method whenever the list of items is modified.

Verifying the saved file

➤ Run the app now and do something that results in a save, such as tapping a row to flip the checkmark, or deleting/adding an item.

Byu Fuhubigfb fovisfahb sap xendiefh a Xmapbpidby.wqoxq gobu
Lra Pewumamld tifedwuxp fud bodjuakz o Frepxmulhl.zfejd lage

Kjiyqdeys.ttolv av Dwiyo
Wkintfovx.mnozv ez Msiri

“NS” objects

Objects whose name start with the “NS” prefix, like NSObject, NSString, or NSCoder, are provided by the Foundation framework. NS stands for NextStep, the operating system from the 1990’s that later became Mac OS X and which also forms the basis of iOS.

Loading the file

Saving is all well and good, but pretty useless by itself. So, let’s also implement the loading of the Checklists.plist file.

Reading data from a file

➤ Switch to ChecklistViewController.swift and add the following new method:

Loading the saved data on app start

Here’s what you need to do:

Initializers

Methods named init are special in Swift. They are only used when you’re creating new objects, to make those new objects ready for use.

Where to go from here?

Checklists is currently at a good spot — you have a major bit of functionality completed and there are no bugs. This is a good time to take a break, put your feet up, and daydream about all the cool apps you’ll soon be writing. It’s also smart to go back and repeat those parts you’re still a bit fuzzy about. Don’t rush through these chapters — there are no prizes for finishing first. Rather than going fast, take your time to truly understand what you’ve been doing.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now