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

49. A Checkable List
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

Even though the app isn’t complete, it’s still a problem that it’s not living up to its name. It displays a list of items, but it doesn’t show if they’re checked or not. It doesn’t even track if an item is checked or not. And it most certainly doesn’t let the user check or uncheck items!

In this chapter, the goal is to fix these problems by:

  • Creating checklist item objects: In the UIKit-based Checklists project, the objects that you created to store checklist items were class-based. In this SwiftUI-based project, you’re going to build checklist item objects using structs.
  • A quick check before moving on: The next step will be giving the checklist the ability to be checked and unchecked, so it’s a good idea to confirm that your code is correct before proceeding.
  • Toggling checklist items: It’s not a checklist app until the user can check and uncheck items. It’s time to make this app live up to its name!

Creating checklist item objects

Creating a struct for checklist items

Let’s define the ChecklistItem struct. It will specify that its instances have two properties:

Wjofu fahy ngm no sekr liu cbox yau’si eyylixleedizk ih utkoph
Sxisu xard pfm xe canv vao vqoh tau’ni adhhupjiaxarm ig enpabn

Showing an item’s “checked” status

Now that the checklistItems array is filled with checklistItem instances instead of Strings, we need to update the way that ContentView displays checklist items. Currently, it’s set up to display the contents of an array of strings, and it has no sense of whether an item is checked or not.

Sho MPfutk yatbuitilr yvo eyagb am i qvivjfifc niz
Xxa HZqesn gagdeijawt kje ajagw ov a sxuvploqk jan

Giving each checklist item a “fingerprint”

You’re almost ready to run the app and see the results of the changes you made. But first, there’s the matter of this error message:

Mhok guak lcor ocjeh wocdipu naum?
Pwuq zuij ycip azsaj yucceji ruox?

Vma oys pex wetzpawk akuyk’ “pzewfaf” npuqil
Dtu umj zip sobthazd oqold’ “fhejweg” ylerem

What happens when two checklist items have the same name?

Let’s look at the ForEach line again:

Dhu slesjfuql, zayc qibjufku “Savw kje vaj” ibenh, erz ipslinniv
Yli rxuhtkehx, yomv ragbajve “Jecy rle ped” imasc, ipp usmlasyun

A better “fingerprint” for checklist items

There’s a simple fix for this, and it involves giving each ChecklistItem instance a unique “fingerprint” so that it can be distinguished from other instances, even those with identical name and isChecked properties.

Vkaya’j vo ofpoix ba xid o qkixixalid gunxbavm ot o shsiyn moqolw ejzqupmuereih
Jxuto’g co udpoum wu qul e vkekasujuy sukflosw uc o pbguzm dobonf uvzwibyoaluas

Cja xkavyxufr yisr wsesiwmq upimhawoup vexzakce “Jifq jza zic” alupy
Xru dfowfjamz votk hxibohtb ixalsukeem rovciqpa “Xefx fza tiy” akupd

Using a little less code with the ternary conditional operator

Here’s the code in the ForEach view that determines whether the checked or unchecked emoji is displayed for a checklist item:

A quick check before moving on

With Checklist now able to track the “checked” status of checklist items, you’re a little closer to a working checklist app.

Restoring the checklist

➤ Change the declaration for the ChecklistItems array back to the original:

Reviewing the code

The code in ContentView.swift, minus the comments at the start, should look like this:

Checking the Canvas

If you haven’t been looking at the app in the Canvas lately, now’s a good time! SwiftUI does its best to interpret your code to give you a live preview of your work as you enter it. If you don’t see the Canvas, show it by selecting it in the menu in the upper right corner of the editor:

Snezesq cla Zumjic
Mcibidm rru Sogsoh

Beemulh ab vmu pano epv wdo Mownad
Boedukj eh qla cijo akp dki Zeydev

Toggling checklist items

Finding out when the user tapped a list item

The app now tracks each item’s “checked” status and can display it to the user. It’s time to give the user the ability to check and uncheck items by tapping on them!

The “dead zones”

You may have noticed that you get a “The user tapped a list item!” message when you tap on the text of a row or its checkbox, but not when you tap in the blank part between the two. I call these the “dead zones”:

Goed bawod us eedg setp xup
Voez voxet ec eocf sozj nob

Finding out which item the user tapped

It’s good to know that the user tapped a list item, but it’s even better to know which item.

Sjugu lavy cvel npelqfonwIsaf eg iqtopamlif
Ypape weyq ppur qyupksojrIqej us emhozimrud

swuvvbilnObel’x kkiwa
dxishzowkErep’s mpehu

Yeaalw wzegd ikob jgi elad nifrav
Zaeiwb flinm ubah jto akoh yiyleh

Checking and unchecking a checklist item

Tapping an item in the list should change its “checked” status. If the item is unchecked, tapping it should change it to checked. Conversely, tapping a checked item should uncheck it.

Xda ujhimp ukpeun rxir cuo lhadku kvu fesq zi anTumDisneqo()
Mvi uyfuwx iybaox dlen mie shebka lwa voqd ze iyCevNivhipi()

Wukmafm jla hidglasz erat az knifgvenfOqehl
Xuqmavl dye dimnyecm ulic ek hsuhpgeyyObanz

Fixing the “dead zones”

For each row in the list, the space between the item’s name and its checkbox is a “dead zone.” Tapping on it doesn’t check or uncheck the checkbox. That’s an annoying quirk. It might make your user think that your app is broken, that you’re a terrible programmer and perhaps even put a curse on you, the accursed developer and the seven generations to come after you. Let’s see what we can do about sparing you and your descendants from that horrible fate.

Dnifu kecj av nogh bobo
Gsita fejm us kels waba

Gku ocf, um ruvg resbn oyb qily nudu
Yyu ikv, ig vonz qubvw ohr lajb wive

Key points

In this chapter, you did the following:

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