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

17. Improved Data Model
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

Everything you’ve done up to this point is all well and good, but your checklists don’t actually contain any to-do items yet. Or rather, if you select a checklist, you see the same old items for every list!

There is no connection between the selected list and the items displayed for that list.

It’s time for you to fix that. You’ll do so by way of the following steps:

  • The new data model: Update the data model so that the to-do items for a list are saved along with the list.
  • Fake it ’til you make it: Add some fake data to test that the new changes work correctly.
  • Do saves differently: Change your data saving strategy so that your data is only saved when the app is paused or terminated, not each time a change is made.
  • Improve the data model: Hand over data saving/loading to the data model itself.

The new data model

So far, the list of to-do items and the actual checklists have been separate from each other.

Let’s change the data model to look like this:

Each Checklist object has an array of ChecklistItem objects
Each Checklist object has an array of ChecklistItem objects

There will still be the lists array that contains all the Checklist objects, but each of these Checklist instances will have its own array of ChecklistItem objects.

The to-do item array

➤ Add a new property to Checklist.swift:

Passing the array

Earlier you fixed prepare(for:sender:) in AllListsViewController.swift so that tapping a row makes the app display ChecklistViewController, passing along the Checklist object that belongs to that row.

Faking it ‘til you make it

Let’s add some fake data to the various Checklist objects so that you can test whether this new design actually works.

Adding fake to-do data

In AllListsViewController’s viewDidLoad() you already put fake Checklist objects into the lists array. It’s time to add something new to this method.

Programming language constructs

For the sake of review, let’s go over the programming language stuff you’ve already seen. Most modern programming languages offer at least the following basic building blocks:

Ooby Vwekhyufx zur tik ubw opp eraln
Oiwt Qjixsgazb poq xeg ehv uvq ivoqz

The new load/save code

Let’s put the load/save code back in. This time you’ll make AllListsViewController do the loading and saving. (We’ll get to how Checklist handles its own loading/saving soon…)

Saving differently

Previously, you saved the data whenever the user changed something: adding a new item, deleting an item and toggling a checkmark all caused Checklists.plist to be re-saved. That used to happen in ChecklistViewController.

Parents and their children

The terms parent and child are common in software development.

The new saving strategy

You may think: ah, I could use a delegate for this. True — and if you thought that, I’m very proud — but instead, we’ll rethink our saving strategy.

Saving changes on app termination

The ideal place for handling app termination notifications is inside the scene delegate. You haven’t spent much time with this object before, but every app has one. It is the delegate object for notifications that concern the app as a whole.

Unwrapping optionals

At the top of SceneDelegate.swift you can see that window is declared as an optional:

Tdi Vizirequah luhbrubluv ey xmu wamvab’g juab fiud towzkiknof
Bva Becusaleap vulzpamked on yti noykuq’f tium quob kakpzukdek

Stoc nza qoet sius kegblurbax ye zxi UxwMinlkSuacRekjnicwor
Lwiy fgo peuv youk tafhyimkor te wla UnfRitynRaalLatkwumhuf

Improving the data model

The above code works, but you can still do a little better. You have made data model objects for Checklist and ChecklistItem but the code for loading and saving the Checklists.plist file currently lives in AllListsViewController. If you want to be a good programming citizen, you should put that in the data model instead.

The DataModel class

I prefer to create a top-level DataModel object for many of my apps. For this app, DataModel will contain the array of Checklist objects. You can move the code for loading and saving data to this new DataModel object as well.

Creating the DataModel object

But where/how does the dataModel instance varaible get populated? There is no place in the code that currently says dataModel = DataModel().

Still confused about var and let?

If var makes a variable and let makes a constant, then why were you able to do this in SceneDelegate.swift:

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