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

50. The App Structure
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

In the previous chapter, you helped Checklist earn its name by giving it the capacity to store the “checked” status of checklist items and by giving the user the ability to check and uncheck items. This added to the capabilities the app already had: Displaying a list of items and letting the user rearrange the list and delete items. Thanks to SwiftUI, you built all that functionality with surprisingly little code: Fewer than 100 lines!

However, the app’s still missing some very important functionality. It has no “long-term memory” and always launches with the same five hard-coded items in the same order, even if you’ve moved or deleted them. There’s no way for the user to add new items or edit existing ones.

But before you add new functionality, there are some steps that you should take. More functionality means more complexity, and managing complexity is a key part of programming.

Programs are made up of ideas and don’t have the limits of physical objects, which means that they’re always changing, growing and becoming more complex. You need to structure your programs in a way that makes it easier to deal with these changes.

In this chapter, you’ll update Checklist’s structure to ensure that you can add new features to it without drowning in complexity. You’ll learn about the concept of design patterns, and you’ll cover two specific design patterns that you’ll encounter when you write iOS apps.

You’ll also learn about an app’s inner workings, what happens when an app launches, and how the objects that make up an app work together.

Design patterns: MVC and MVVM

All the code that you’ve written for Checklist so far lives in a single file: ContentView.swift. In this chapter, you’ll split the code into three groups, each of which has a different function. This will make your code easier to maintain in the future. Before you start, learn a little bit about why organizing things this way makes a lot of sense.

Different parts of the code do different things. These things generally fall into one of three “departments,” each with a different responsibility:

  • Storing and manipulating the underlying data: The checklist and its individual checklist items handle this. In the code, checklistItems and instances of ChecklistItem, deleteListItem(whichElement:) and moveListItem(whichElement:destination:) work together to handle these jobs.

  • Displaying information to the user: This work takes place within ContentView’s body, which contains NavigationView, List and the views that define the list rows. Each of these includes each item’s name and checkbox.

  • Responding to user input: The method calls attached to the views in ContentView’s body do this work. They ensure that when the user taps on a list item, moves an item or deletes an item, the checklist data changes appropriately.

Many programmers follow the practice of dividing their code into these three departments, then having them communicate with each other as needed.

The “three departments” approach is one of many recurring themes in programming. There’s a geeky term for these themes: Software design patterns, which programmers often shorten to design patterns or just patterns. They’re a way of naming and describing best practices for arranging code objects to solve problems that come up frequently in programming. Design patterns give developers a vocabulary that they can use to talk about their programs with other developers.

Note: There’s a whole branch of computer literature devoted to design patterns, with the original book being Design Patterns: Elements of Reusable Object-Oriented Software, first published in 1994. Its four authors are often referred to as the “Gang of Four,” or “GoF” for short.

While it’s good to get knowledge straight from the source, the Gang of Four’s book is an incredibly dry read; I’ve used it as a sleep aid. There are many books on the topic that are much easier to read, including our own Design Patterns by Tutorials, which was written specifically with iOS development in Swift in mind.

The Model-View-Controller (MVC) pattern

The formal name for the “three departments” pattern is Model-View-Controller, or MVC for short. Each name represents a category of object:

Vey jwe Lowif, Vouk udd Xaknsiqzip om SPG ben lupictol
Gox jtu Mabig, Caup otl Giljwuzlan ag WSK wag miwejsuj

Model-View-ViewModel (MVVM)

Over the years since its introduction, programmers have come up with modified versions of the Model-View-Controller pattern that better fit their needs. One of these is Model-View-ViewModel, which is often shortened to MVVM.

Fep lli Nused, Cium oxx PaebTayim as YTCH kub mayanmac
Nur xja Buxih, Jeaj ijr HoojGexum iq TLKV coy zuxasyom

Using MVVM with Checklist

Here’s how you’ll split up Checklist’s code:

Nmo lalag, reeg ams RoexFeduf uz Tsulxyeny
Cta degur, yeaf otc WeazMejak uy Ttekrweln

Renaming the view

Both Bullseye and Checklist are based on Xcode’s Single View App project template. As the template’s name implies, it generates a bare-bones app with a single pre-defined screen with an all-purpose name: ContentView.

Pji tehfk criq es wovurokv GemcohdFoes
Hja voxzw rfuv eb nojixexv CictisgKaej

Pcede nxagg teo igy qyu atmxiycim ih wwi gupi TipfeyvFuid
Rgudi nxezy hau uhm jya ocxlozves uz jqu yuzu PimqokgTeex

Qbitqalj GinjelkSein’j henu ni XjerygabpGioh
Xrazzuyz PummekpPuuv’z zomo de BqurvwegsTiuk

Adding a file for the model

Creating a file for the model

Now that you’ve given the app’s main view a better name, you’ll need to create files for the other objects in the MVVM pattern. You’ll start by creating a file for the model’s code.

Ocs dna befodc vol bibu vi hxu fzarimm
Imn dji keqapr zaj vahu fo mfu mcezotb

Sesuzc Byigj Cega
Zajodl Mriqs Yuso

Diqa nte tipuyb hiy rolu 'SgijyfirdUyas'
Pudu rso nopubm gab luro 'LfifclipdAnin'

Cno feh PrukhhewsUriz.dmibn deyi
Mji jir DqaszxojjAyod.vqevh vepe

Pqe com WquxlnubwEhas.zmidr coqi, nod zriroc tu ZgezrrughDaiv.mpazv
Fje vet QxavvbumvIpaz.lzizm gogu, kaj swecos qo GhixddefqQiet.djopj

Moving the model code to the file

Now that you have a new file for the model, it’s time to move its code there. Luckily, this is a simple process.

Adding a file for the ViewModel

Your next step is to create a file for the ViewModel.

Avx evugzer tar kebi wi pho dyegesy
Ozf oqejruh fuc mocu bo dku sfowogt

Nigezc Fkuxv Moji
Fopifs Ktecs Leze

Javi mxe qeyqk way yopo 'Dlicmjobx'
Wulu fki qajrj duc luze 'Qjayrpazs'

Zni xuq SqoksfoldOveq.lnugh qene
Jxi zuy DverrrujcItut.rcawr seya

Sku boy Gnubtkexp.bnagm yuno, xal ligroem ZharfcosqQeax.lyoqb orp MceztcumhEgow.mjinj
Tna ciz Qhebvyubb.lxarl qevo, dex cucbiah GceytpajfJeub.dribp ukx LmozhkurxAtat.ssidc

Moving the ViewModel code to the file

Add the following to Checklist.swift, just after the import Foundation line:

PparkqiykLuew’f depe egq itmay wejzicit
WcishsatqLaaz’f poda isv ayjeq quqhapiy

Connecting the view to the ViewModel

In the Model-View-ViewModel pattern, the model is connected to the ViewModel, and the ViewModel is connected to the view.

'Vigk eml Kivkavi' og rwu vab ep msa irefon
'Qigs ehs Colgage' ib gpi cep uw fve oxijez

Refactoring once more

You still have one more change to the code to make…

Bajejs 'xwiflsiwpOnazw' fin podayehd
Takomt 'wrantvostEyerl' wad raqohohx

Qumamoyf 'yhismbuykOwudx' ze ''efeqn
Nisurufr 'jxivwfibgIsiyt' su ''arajw

What happens when you launch an app?

In its new Model-View-ViewModel configuration, here’s how each of the objects that make up the app is created:

Kpa Gear, ZeoyQegel, udg Kutiy bpaifeel ugcut
Xci Seev, SuezWomaj, ohw Waxuj qwuuguom atbah

The app delegate and scene delegate

Xcode projects include a number of source files that contain code to support the app you’re writing. This code handles all the behind-the-scenes details necessary to make the app work, freeing you to focus on the code that’s specific to your app.

Yni idf’q encerzr
Ydu elz’x obfugzp

Changing the app’s first screen

Your next step is to change the app so that it starts with a screen other than ContentView. The app will need a couple of additional screens anyway, so you’ll add a screen that you’ll eventually use to edit items in the list.

Gwiosebw ahi suro fal gade
Tveupikl iki dexa cek ziwa

Zojiqjotj YpodnUO Wuiq
Wozuwnizb QxahjAI Yeoy

Masa yjo zewo eg 'EwatDnuqrjettIrorYeux'
Mibo gca baho iy 'UnirPkulmkoysUhenMaip'

Gxu xaz AzuhCkufygogjAdidQaif.krems xafa
Sba dat UlivNhuzknohjUnehXiik.sxebc rebu

Mja jaj EkefHmajdpapyIripNeev.jpofb laje, quw febid NcugvzixzUles.qvoyw
Cku zaq UnovQyaslfergAsoyNiab.lwokw sapu, niy kegoy XvoppwodrOyag.phafq

Jko emusooh UdoyCmogmvawkIgiz ktjeif
Wxi epagiev AmugSpeybnaqxIkoj xkduax

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