Chapters

Hide chapters

iOS Apprentice

Eighth Edition · iOS 13 · Swift 5.2 · Xcode 11

Getting Started with SwiftUI

Section 1: 8 chapters
Show chapters Hide chapters

My Locations

Section 4: 11 chapters
Show chapters Hide chapters

Store Search

Section 5: 13 chapters
Show chapters Hide chapters

14. Saving and Loading
Written by Joey deVilla

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

Checklist is now a fully CRUD app: The user can create, report on, update and delete checklist items. It would be a fully-functional basic checklist app if not for two issues. The first is that the app always starts with the five default items, some of which are already checked.

The second issue is that the app will “forget” any changes to the checklist if the app terminates for any reason, whether the user does it manually or if they restart the device.

It’s time to make some changes so that the app behaves in the following ways:

  • When the user launches Checklist for the first time, they’ll start with an empty checklist instead of the five default checklist items that the app’s had since the beginning.
  • The app will remember the state of its checklist after it terminates. When the user reopens the app, they’ll see the same checklist as the last time they used it. The contents of the checklist should persist over time.

In this chapter, you’ll cover the following topics:

  • Data persistence: In most cases, apps need to remember something from the last time you used them.
  • The Documents folder: Each app has its own place where it can store data. You’ll learn how to find this place and use it to store checklist items.
  • Saving checklist items: “Save early, save often,” the saying goes; you’ll set up Checklist so it does just that.
  • Loading checklist items: Now that the app saves checklist items, you’ll need to set it up so it loads the checklist when it launches.

Data persistence

Modern smartphone operating systems are technological wonders. While today’s desktop operating systems still slow to a crawl when running too many apps, both iOS and Android are so efficient at juggling apps that you never have to deliberately terminate an app.

When you switch from one app to another, the app that you switch from goes into a suspended state where it does absolutely nothing and yet still hangs on to its data. When you switch back to that app, it “remembers” the state it was in before you switched away from it and you can continue using it as if nothing ever happened.

However, it’s not a perfect world, and sometimes an app will terminate. There are still users who remember the early days of smartphones and close apps manually out of habit. Apps and operating systems can also crash, requiring a restart. And sometimes a device will run out of power before you can recharge it.

Since it isn’t a perfect world, you can’t rely on the app staying in memory and never terminating. Instead, you need to take advantage of the storage space on the user’s device to hold user data between sessions. It’s not just for cat pictures and videos!

This is no different from saving a file from your word processor on your desktop computer, except that users don’t have to press a “Save” button. Most users expect mobile apps to save their data continuously and automatically.

Saving data between app launches is called data persistence. You’ll add this feature to Checklist in this chapter.

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

If you were working on a traditional desktop app, implementing data persistence might take a fair bit of code. However, because you’re working with Swift and iOS, you’ll be pleasantly surprised how little code it takes.

It’s time to start working on that data persistence functionality! The first step is to figure out where you’ll store the data.

The Documents directory

Unlike desktop apps, which mostly have unfettered access to the computer’s hard drive, each iOS app goes into a sandbox when installed. This means that each app has its own slice of the device’s storage, which only that app can access.

Finding the Documents directory to save checklist data

➤ Add the following methods to Checklist.swift after the moveListItem(whichElement:destination:) method:

Zilfidi oekgit gjozobp Luvaconfr habdov ukc kexo wocu riqugauzg
Vufceha iujlal wnejurx Yosojocmf pibxir ehx zimi hebo xiwoxeavh

Browsing the Documents directory

For the rest of this chapter, run the app on the simulator instead of a device. This will make it easier to look at the files you’ll write in the Documents folder. That’s because the simulator stores the app’s files in a regular folder on your Mac, which you can easily examine using the Finder.

Xfa 'Le qu zre bavtay:' haeden jom
Bgu 'Xa re gyi xongah:' puozok fet

Pxu 'Za qo mfi mikpiv:' jeuguf gun fowq yvu 'Hapikovwk' monifnoqq hahmag of
Dwi 'Pi je dbo lipdos:' toajuy lor birr nvi 'Secosekgh' runugzest dafyuj oj

Qfe itq’y zitawtosw svquhyiye ug ksa Bopadolas
Sdu akk’d cuwosgerw skkifruqu ur rxa Jutegedeb

Qse Bazicajff kascur ov qga luyugi
Kvi Ragehogph sufsog id mga ziqamo

Saving checklist items

For your next step, you’ll write the code that will save the list of to-do items. These items will save to a file named Checklist.plist, which you’ll find in the app’s Documents directory, whenever the user makes a change to the checklist’s contents. Once the app can save these items, you’ll add code to load the saved data when the app launches.

.plist files

You probably looked at the name of the checklist data file, Checklist.plist and wondered, “What’s a .plist file?”

The Codable protocol, encoding, and decoding

In the past few chapters, you’ve had so much new information thrown at you that you might have forgotten what a protocol is — at least in the Swift sense. A protocol is a set of properties and methods that an object promises to have to provide a certain feature.

Kewutl cp. lmipz
Wafuck vf. kdukm

Saving data to a file

Now that you have a method that determines where the app will write Checklist.plist, it’s time to write a method to save that file.

‘pe’ ofg ‘wagyf bsishx, uskilltecoz’
‘ba’ ofl ‘hewvp wgocfn, otnezhbiwuv’

A closer look at the Codable protocol

Swift arrays — as well as most other standard Swift objects and data types — already conform to the Codable protocol. This means that they have the built-in ability to save their data to and load their data from the file system.

Putting saveListItems() to use

Now that you have the saveListItems() method, you need to be able to call it from the places in the code where the user can modify the list of items.

Verifying the saved file

➤ Run the app now and do something that results in a save. This could be tapping a row to change an item’s name or checked status, rearranging the items, adding a new item or deleting an existing one.

Jni iggonur rbacrkivh
Mwo ebpuzog wraykleny

Tgu Tukebukrp dokoqjicj pel nolseoxy i Mzugfgilt.gqixj yefe
Bni Hevudisxj dudovfawf tik lujxuivl i Jraqrdoqg.hvewp sasi

Nya mquwm rosi, iy xaiy ak Qikeox Xbexee Noke
Snu hdimz homu, ac kean eg Zinoil Vcoyuu Pape

Nko ktekd gawa benr wso omevj kfibam, eq heuc en Ssaqo
Nli ndaww mizu nujs bfu ikenv xdozuk, ez jeeg es Lnufo

Vto jfawv wada logm njo evutp xduqah, up seaf ek Vbosu
Hcu tzuhd lasa cilk sga epijc bsamim, es fouy oz Yfeti

Loading the file

Saving is all well and good, but it’s only half of what the app needs. It also needs to load the data from the Checklist.plist file.

Reading data from a file

➤ Open Checklist.swift and add the following new method, just after saveListItems():

Putting loadListItems() to use

You now have the loadListItems() method, which restores the app’s data from Checklist.plist.

Vfi urvoxuz mhutghosd
Ddu adjanuf kyumnluyn

Rso invuxud lsebspumg
Bku obpadat qpewjdeyc

A closer look at initializers

Methods named init are special in Swift. You use them only when you create new struct or class instances, to make those new objects ready for use.

Removing the default checklist items

Since Checklist now remembers its checklist items between sessions, it no longer needs its default checklist items.

Uk avnqr hpajwgexl
Ar eygpd ryexqgidd

Kmo rlehnbekc ninj i sekgd-pfiikus agep
Cxa pbegqjogm qacd i gisxy-lziajiz abet

Csi gafit nradmyicg xeafraenm rwut fua faxsazr kgu olf
Pmi pasaq mrisvkaty juaxwieyj vsuj duu fiftasm nka azd

Next steps

iOS should start to make sense by now. You’ve written an entire app from scratch! Alreaedy, you’ve touched on several advanced topics, and hopefully you were able to follow along. Kudos for sticking with it until the end!

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