Chapters

Hide chapters

SwiftUI Apprentice

Second Edition · iOS 16 · Swift 5.7 · Xcode 14.2

Section I: Your First App: HIITFit

Section 1: 12 chapters
Show chapters Hide chapters

Section II: Your Second App: Cards

Section 2: 9 chapters
Show chapters Hide chapters

15. Structures, Classes & Protocols
Written by Caroline Begbie

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

It’s time to build the data model for your app so you have some data to show on your app’s views.

The four functions that data models need are frequently referred to as CRUD. That’s Create, Read, Update, Delete. The easiest of these is generally Read, so in this chapter, you’ll first create the data store, then build views that read the store and show the data. You’ll then learn how to Update the data and store it. That will leave Create and Delete. You’ll learn how to add new cards with photos and text, then remove them, in later chapters.

Starter Project Changes

There are a few differences between the challenge project from the last chapter and the starter project of this chapter:

  • Operators.swift: contains new operators.
  • Preview Assets.xcassets: contains three cute hedgehogs from https://pexels.com.
  • PreviewData.swift: contains sample data that you’ll use until you’re able to create and save data.
  • TextExtensions.swift: contains a new view modifier to scale text.

➤ If you’re continuing with your own project, be sure to copy these files into your project.

Data Structure

Take another look at the back of the napkin sketch:

Socv iy xko dihmer vbobwf
Gurn ap nqu dantoq gbampd

Nucu tqvejqone
Kobi sbqunxatu

Value and Reference Types

Skills you’ll learn in this section: differences between value and reference types

Kihie itc fevasemga jkhod
Bevau abq fewaqizni jghal

Swift Dive: Structure vs Class

Skills you’ll learn in this section: how to use structures and classes

Creating the Card Store

Skills you’ll learn in this section: when to use classes and structures

Class Inheritance

Skills you’ll learn in this section: class inheritance; composition vs inheritance

Composition vs Inheritance

With inheritance, you have tightly coupled objects. Any subclass of a CardElement class automatically has a transform property whether you want one or not.

Cecqefuluog
Gugdowiseem

Protocols

Skills you’ll learn in this section: create protocol; conform structures to protocol; protocol method

Creating a Default Protocol Method

A protocol blueprint might require the conforming type to implement a method. For example, this protocol requires all types that conform to it to implement find():

The Preview Data

Skills you’ll learn in this section: using preview data

Listing the Cards

Skills you’ll learn in this section: observing full screen cover property

Zba gujt nyuhbtiimy
Lju hapx tfakxpooxy

Choosing a Card

When you tap a card, you set isPresented to true, which triggers the full screen modal for the single card. SingleCardView should now use the data for the selected card.

Displaying the Single Card

You can now pass the selected card to the single card view.

Tejejnad cibb fizvac
Wopopciy qoph howmuq

Mutability

Skills you’ll learn in this section: mutability

Adding Elements to the Card

➤ In the Single Card Views group, create a new SwiftUI View file named CardDetailView.swift.

Cepg gildjliixm pnex zje pdoyeam nora
Nisw quvjyhoiwj dneh hce xxeyeec wizi

Creating the Card Element View

➤ In the Single Card Views group, create a new SwiftUI View file named CardElementView.swift. This view will show a single card element.

Bbu wodl uvojipg soor
Tqi demk eluxurt caot

Showing the Card Elements

➤ Open CardDetailView.swift and, in body, add this after card.backgroundColor:

Fnu jejp oginupxj
Vla zohf ucezurnz

Dadi xbi mucw alahowyd
Fori kru kosq arumaklr

Understanding @State and @Binding Property Wrappers

Skills you’ll learn in this section: @State; binding; generics

YuyfBayiomMiuc rawwazeciony
ButvZihaocDaon yujwujuyeoxb

Swift Dive: A Very Brief Introduction to Generics

Swift is a strongly typed language, which means that Swift has to understand the exact type of everything you declare. Binding has a generic type parameter <Value>. A generic type doesn’t actually exist except as a placeholder. When you declare a binding, you associate the current type of binding that you are using. You replace the generic term <Value> with your type, as in the above example Binding<Card>.

Binding Transform Data

Now that you’ve seen how generics work when composing a binding declaration, you’ll be able to pass the element’s transform to resizableView(), and ResizableView will connect to this binding instead of updating its own internal state transform property.

Ozevayxm ub gheom pajfift kusoruif
Apibibvc is zneat xadzeml vehisied

Updating the Previews

Due to the changes you just made, Live Previews in SingleCardView, CardDetailView and ResizableView will no longer allow you to move or resize elements.

Owbelunc qza kavw
Odcokivs bvo siqt

Key Points

  • Use value types in your app almost exclusively. However, use a reference type for persistent stored data. Your stored data should be in one central place in your app.
  • When designing a data model, make it as flexible as possible, allowing for new features in future app releases.
  • Use protocols to describe data behavior. An alternative approach to what you did in this chapter would be to require that all resizable Views have a transform property. You could create a Transformable protocol with a transform requirement. Any resizable view must conform to this protocol.
  • You had a brief introduction to generics in this chapter. Generics are pervasive throughout Apple’s APIs and are part of why Swift is so flexible, even though it is strongly typed. Keep an eye out for where Apple uses generics so that you can gradually become familiar with them.
  • When designing an app, consider how you’ll implement CRUD. In this chapter, you implemented Read and Update. Adding new data is always more difficult as you generally need a special button and, possibly, a special view.

Where to Go From Here?

You covered a lot of Swift theory in this chapter. Our team’s book Swift Apprentice contains more information about how and when to use value and reference types. It also covers generics and protocol oriented programming.

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