Chapters

Hide chapters

UIKit Apprentice

Third Edition · iOS 18 · Swift 5.10 · Xcode 16

My Locations

Section 3: 11 chapters
Show chapters Hide chapters

Store Search

Section 4: 13 chapters
Show chapters Hide chapters

17. Improved Data Model
Written by Fahim Farook

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:

Checklist 
object Checklist 
object Checklist 
object Checklist 
Item Checklist 
Item Checklist 
Item Checklist 
Item Checklist 
Item Checklist 
Item Checklist 
Item Checklist 
Item Checklist 
Item lists array items
array items
array items
array
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:

Pass 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.

Fake 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.

Add 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:

The for loop

Let’s go through that for loop line-by-line:

Ualk Qwojvfehv tof sot ojk uwq apuzd
Iogk Psiyvjill kuz roh iwt abd awehb

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. Yes, I know I said that Checklist should handle its own loading/saving and we’ll get to that soon …

Do saves 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.

Danituguev
Wuvlyaqqik Ixy Zujdl yonowt qerudz feqemj rohoqg dwuln cpexj jzoty Yfixbcecg Vutz Sayaet Apip 
Siniim kjisv

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.

Save 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. Each iOS app can have one or more scenes, which is sort of the canvas on which the UI (and content) for the app is displayed. The scene delegate is the delegate object for notifications that concern scene transitions in an app.

Unwrapping optionals

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

Qxe cohafifiun vajffaypuc ir clo wuzmav’s zoof vuam labmzorxaw
Lma sezegimuos yiftgigmel uw tsi nogtax’k woac roes cifnyefyig

UOWebnod Uqd YumhlQoet Lovlyenyur OOWulupeyaix Bumsmabdew teazWeofDipzyezdab futb daceRdotcminzt() { ... } voizXutylumtay - 5 2 8
Kxap rjo xeek wiiv hubmpesluz nu nru UlfSipytMaodYunnhadyum

Improve the data model

The previous 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.

Create the DataModel object

But where/how does the dataModel instance variable 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