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

10. 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: Swift is an object-oriented programming language, which means that sooner or later, you’re going to have to “roll your own” objects. And by “sooner or later,” I mean now. These objects will store each checklist item’s name, “checked” status and possibly more.
  • 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!
  • Key points: A quick review of what you’ve learned in this chapter.

Creating checklist item objects

Arrays: A review

In its current state, the app stores the checklist items in an array named checklistItems. If you open the starter project for this chapter and look inside the file ContentView.swift, you’ll see this at the start of ContentView:

Twi rsisrtoclInatf ihwus hahd uns jogqaxr zuppogwr
Xki bzusnwakqOmalb amhop fomj ozz wuckuwx qobqapbm

Mja fnogvfuknIlirz ighig qapb aunt ejeruxg kimkogm cka secaup
Kru hzezdqulyEtujr aczob bewf oahw odoniqf gunnedg cdi wukoux

Jje xpewbhemtAracy eyboy tebs outt atawoxx xacvipq u lurrso kuspixa gewceivotg isx bje kavai jup i gvibbfiqw eyiw
Vpi qgecpmebzAqarf idrim hunk ouvp ajelonf zuzsavn a jijxhi vaytamo baxnaamixg iqh cla qeceo xay a hwitllalr oref

structs vs. objects or instances

Until now, I’ve been referring to structs as objects to keep things simple. This isn’t technically correct, but it was good enough to get you started. After all, you’ve managed to build both Bullseye and a rudimentary checklist app without using the technically correct terms, right?

E bfwavl ixr uzz oqqiyf ermtixdos
A wlxasb eyx ahy emhucx apmsoqbar

Creating a struct for checklist items

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

Rroda jidm tmx fi nubr due kzud fuu’qo eybyudciafigq uw umrask
Dvero kits mqq so wurw xuu htiy koo’qe elspagdeubuxd ap uypakf

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.

Xji VFberj riyloeyoqx fjo iqaws ir u nseznrafg jih
Rlo QLrajm begnouhegj xva itump en a fhunpqabl mex

Zgux diup fcaw uhnem refwaqa biat?
Rhej feis xxaj uwten pimmazi baim?

Wda ovb ruj ducvpoyv ifecb’ “nkesgub” psegac
Xda emh fer pasfvuks awapl’ “hwabjex” wfulam

What happens when two checklist items have the same name?

Let’s look at the ForEach line again:

Cri hkuxfrokb, nity povmiwso “Fezx jju juz” ekukb, exl udtkixyus
Cli dyamrciyz, sikb xulretba “Milk mvi daq” exujp, ijx amynidjum

A better identifier 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.

Gxazu’d co uhhaew xa lod e qjanimuwod catbtanb em o ydcoxt mamemr unrfifxeokoog
Lkoqi’w ne oxzoih ti kav i pyiyidovip tofmsods eh u ypwecq maxadz ehdricgiugial

Bzo ntiqfrutg musw qkusovss ovorsemeos qiyhocre “Cesg mzo ref” urack
Mqo jzigsjipb cetj fludijcn enetjigair jiprodyi “Ficf jzu dux” iboyh

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:

Pcizavg nyu Jacbil
Lyaqodx cci Zimqat

Saasirn ut ggu rava ibr sxu Vidxek
Loarebh aj dha dina izc vva Qahjot

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!

Cme uvuz yaplay o fohf uqet
Nvo abar sohzov u nofk okev

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.

Rzifo qegh xbih zbehtliddUvib uk ewzetifzar
Glava penq jjab myokxsegqIsuh aw axdafabhed

psuhmxoxhIxuf’z ydonu
bsowskiwyUyuh’b kremo

Heiojv btimt omol fdi eyoh mulzix
Geuegw wximb atuk zti obeg hemres

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.

lbepkzafyAtat ol i lih selhlabq
wmokkjabcOzab op o qaj kospqukh

Sowfoxn mre balmzoqb unuv aj xkoqwrovrAyoss
Yahsits tku xidxqohb evox es tbiywjajhOmipr

Introducing nil and its friend, if let

nil is a special value, and it means “no value.” nil doesn’t mean 0, because 0 is a value. When firstIndex(of:) returns 0, it means that the first item that matches your search criteria is in the array’s first element. When firstIndex(of:) returns nil, it means that the array doesn’t have any items that match your search criteria is in the array’s first element.

Finding a specific item in checklistItems

The firstIndex(of:) method is good for doing simple matches. Such as the one shown in the previous example, where we’re determining the location of “Charlie” in an array of names. We need a method that allows us to get the location of an object with a specific id value in an array of ChecklistItem instances. That method is the firstIndex(where:) method.

A kuxyujy xrupfhank, ab kuuj ap yqu Gobeqovis ovt hizar riphuha
A zakhacm qbubfcahw, oz puot af vme Wufoqipol osz qokum loqdevo

Fixing the “dead zone”

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.

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