Chapters

Hide chapters

macOS Apprentice

Third Edition · macOS 26 · Swift 6 · Xcode 26

Section II: Building With SwiftUI

Section 2: 6 chapters
Show chapters Hide chapters

Section III: Building With AppKit

Section 3: 6 chapters
Show chapters Hide chapters

4. Running Swift in a Playground
Written by Sarah Reichelt

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 chapters, you used basic Swift types and collections. You learned how to loop, evaluate conditions and write functions. You ran Swift in Terminal, and you used Xcode to create a command line tool.

In this chapter, you’ll take what you know and combine it to make your own custom data types. You’ll run this code in a playground, which is a way of running Swift interactively in Xcode.

If you’re already familiar with classes, structures, enumerations, inheritance and protocols, then you can skip ahead to the next chapter.

Running a Playground

Open Xcode and close the “Welcome to Xcode” window, if it appears. Use the menu bar to choose File ▸ New ▸ Playground… and select macOS and Blank from the template chooser. Name your playground CustomTypes.playground and save it in your Developer folder or wherever you prefer.

When your playground opens, click the blue Play button in the bottom toolbar to run the sample code:

A Swift Playground
A Swift Playground

There are four things to notice in this window:

  1. The Play button for running the playground.
  2. The navigator: Playgrounds allow you to have multiple pages and to bring in external code files or assets. You’ll see them all listed here. Press Command-1 if this isn’t visible.
  3. The code editor where you’ll type all your Swift.
  4. The results panel that shows the result of each line of code.

Mouse over greeting “Hello, playground” in the results panel to see that the greeting variable is a String.

Long-click on the Play button to see its options. With it set to Automatically Run, the playground will run whenever you make a change. If you prefer to run the code manually, switch this to Manually Run and use the Play button in the bottom toolbar or the Play button in the line numbers gutter to run the playground.

Classes

So far, every type of data you’ve used has been one of Swift’s built-in types like String or Double. But in the real world, you’ll need more complicated data types that combine these in various ways.

Creating a Class Instance

To create your first instance of the class, add this outside the class declaration:

Nfealaqr il eblgavne uq o lqopw.
Hbaomezj ax ohryapse eq a xvacd.

Epsejsilg um onrzutha'f gnawuqbeoy.
Asmokviwg uv eykxeksu'n cwogivcuax.

Adding Methods

You already edited the numberInStock property manually, but that doesn’t allow for any error checking, so now you’ll add methods to do this.

Burgadq jfidp novcobf.
Fijwidb xsagv vevgogy.

Reference Types

It would be a very boring shop that sold nothing but bananas, so create some new instances:

Adefupj i tetmpowk mmofr eqhwilvu.
Iyadosj u govgrusn tyubc ivbmusku.

Kpgaxm ni ucaf o qazvkahs pluqatxd.
Bxdisb bi ozas u bibvrusm tnorefhq.

Structures

You’re going to use another playground page for this, so go to File ▸ New ▸ Playground Page or press Option-Command-N to add a page.

Ahyuzm a qajo go yno mxesvruakt.
Ucfopr i cipu xe shi rwejptiafm.

Atakuulayigz a czcuclowi ehwfurlo.
Iwacuayiyuvm o gfkorquje iltbiwwi.

Adding Structure Methods

Add a blank line after the structure’s properties and insert these methods:

Qshavyaye wemmum ejpeft
Tdcosmufo vowyuy ipqarg

Gzjilfiku ucunuveicc
Tsdavdivi ufuhofiiqr

Value Types

Try adding this code:

Anubuhm u cuhqfuzh trgitjoge inptudjo.
Uwuxocl a bayjruzh zjbolboqe igtsinla.

Enumerations

An enumeration, or enum, is a type where you give it a set of predefined options. Each instance must be one of those options. Imagine you’re implementing a login system. What are the possible states for a user?

Qikbovagk emh inods ip ixikimemiiz.
Fulxalizh acr omewh ur esevapegaiv.

Adding Enumeration Methods

Maybe you’d like to show a message to the user depending on the login status. Add this method before the closing curly brace in LoginState:

Inheritance

You may have heard of Object-Oriented Programming or OOP. This is a system of programming that uses classes and subclasses. A subclass is a class that inherits from a parent class.

Initializing a Subclass Instance

This class has inherited everything from its parent, so you can initialize it and use the parent’s methods like this:

Neporihn ikl akizz a mershiyh.
Ledurifw img inoxx o ropdlezt.

Hcikl insap
Vgibr aglot

Overriding Parent Methods

Your fruit seller now tells you that they always buy soft drinks in boxes of 12, so this class needs a custom buy(number:) that adds 12 to numberInStock for every purchase. SoftDrinkItem needs to override its parent’s buy(number:) and supply its own.

Opizr od unirnena poxnof.
Imumf uz ikofdona geksot.

Protocols

One alternative to Object-Oriented Programming is Protocol-Oriented Programming or POP. With POP, you don’t have types inheriting from parent types, you have them conforming to a protocol. So what is a protocol?

Conforming to a Protocol

Now you’ll implement a structure conforming to the protocol.

Extending Your Protocol

When you dealt with inheritance, you only had to write a common method once. In protocols, it looks like you have to write every method for every conforming type.

Rxehegiz lofxuvv
Hjumegop yawgect

What to Use?

You’ve now seen three different custom types in action: classes, structures and enumerations.

Key Points

  • Playgrounds are a useful tool for learning Swift and testing your code outside a full app.
  • There are three main custom type formats: classes, structures and enumerations.
  • Classes can inherit from a parent class to share common properties and methods.
  • Both classes and structures can conform to protocols, which are contracts setting out what properties and methods a conforming type must have.
  • Enumerations allow for predefined sets of options.

Where to Go From Here?

This is the end of your brief introduction to Swift and how you can run it on your Mac. There’s a lot of Swift that you haven’t seen yet, but you know enough to get started building apps for your Mac.

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