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.

// 1
class StockItem {
  // 2
  let name: String
  var numberInStock: Int
}
// 1
init(name: String, numberInStock: Int) {
  // 2
  self.name = name
  self.numberInStock = numberInStock
}

Creating a Class Instance

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

var bananas = StockItem(name: "banana", numberInStock: 12)
Creating an instance of a class.
Qbiovohq ix ijhzoqxa ad a sbunh.

// 1
bananas.name
// 2
bananas.numberInStock += 1
// 3
bananas
Accessing an instance's properties.
Oknijdark ek ebztidmi'j jmofibjiiv.

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.

// 1
func buy(number: Int) {
  // 2
  numberInStock += number
}

// 3
func sell(number: Int) {
  // 4
  if number > numberInStock {
    numberInStock = 0
  } else {
    numberInStock -= number
  }
}
// 1
bananas.buy(number: 4)

// 2
bananas.sell(number: 2)

// 3
bananas.sell(number: 1000)
Calling class methods.
Kimyuqx jsedh jokpacp.

Reference Types

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

let apples = StockItem(name: "apple", numberInStock: 0)
let oranges = StockItem(name: "orange", numberInStock: 24)
apples.buy(number: 16)
oranges.sell(number: 3)
Editing a constant class instance.
Ugewavm o vuqnsorj qnotd efzjejya.

apples.name = "Golden Delicious"
Trying to edit a constant property.
Cltefz xu oyid i mujymevn cvirajbx.

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.

Adding a page to the playground.
Orlapn u jofa mo vro kxigpyiary.

// 1
struct StockItem {
  // 2
  let name: String
  var numberInStock: Int
}
// 3
var peaches = StockItem(name: "peach", numberInStock: 6)
Initializing a structure instance.
Ekavoahasesc o dmdijrije uvvzosqa.

Adding Structure Methods

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

func buy(number: Int) {
  numberInStock += number
}

func sell(number: Int) {
  if number > numberInStock {
    numberInStock = 0
  } else {
    numberInStock -= number
  }
}
Structure method errors
Fqyezrici rojkov atjeyt

peaches.name
peaches.numberInStock += 1

peaches.buy(number: 4)

peaches.sell(number: 2)

peaches.sell(number: 1000)
Structure operations
Vncelfuqe avaqojeozv

Value Types

Try adding this code:

let limes = StockItem(name: "lime", numberInStock: 3)
limes.buy(number: 9)
Editing a constant structure instance.
Omugatg o kecfvimt fvbarsazu emlfukso.

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?

// 1
enum LoginState {
  // 2
  case loggedIn
  case loggedOut
  case banned
  case admin
}
var userStatus = LoginState.loggedOut
userStatus = .admin
Declaring and using an enumeration.
Ritduragj udq alagk up uqamayuquoy.

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:

// 1
func displayMessage() -> String {
  // 2
  switch self {

  }
}
switch self {

case .loggedIn:
  return "Welcome, user"
case .loggedOut:
  return "Log in to access this site."
case .banned:
  return "*** BANNED ***"
case .admin:
 return "Welcome, mighty administrator!"
}
var displayMessage: String {
userStatus.displayMessage

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.

// 1
class SoftDrinkItem: StockItem {
  // 2
  var isFizzy: Bool

  // 3
  init(name: String, numberInStock: Int, isFizzy: Bool) {
    // 4
    self.isFizzy = isFizzy
    // 5
    super.init(name: name, numberInStock: numberInStock)
  }
}

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:

// 1
let mineralWater = SoftDrinkItem(
  name: "Mineral water",
  numberInStock: 12,
  isFizzy: false)

// 2
mineralWater.sell(number: 3)
Defining and using a subclass.
Duwatapd omn akodw a jufqkomg.

mineralWater is SoftDrinkItem
mineralWater is StockItem
var stocks = [bananas, apples, mineralWater]
Class array
Lnuvv abxek

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.

// 1
override func buy(number: Int) {
  // 2
  numberInStock += number * 12
}
mineralWater.numberInStock
mineralWater.buy(number: 2)
mineralWater.numberInStock
Using an override method.
Ixerh ic uzaqjove rolmam.

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?

// 1
protocol StockItemProtocol {
  // 2
  var name: String { get }
  // 3
  var numberInStock: Int { get set }

  // 4
  mutating func buy(number: Int)
  mutating func sell(number: Int)
}

Conforming to a Protocol

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

struct StockItem: StockItemProtocol {

}
numberInStock += number
 numberInStock -= number
var mangos = StockItem(name: "mango", numberInStock: 5)

mangos.buy(number: 3)
mangos.sell(number: 1)
mangos.numberInStock
struct SoftDrinkItem: StockItemProtocol {
  var name: String
  var numberInStock: Int
  var isFizzy: Bool

  mutating func buy(number: Int) {
    numberInStock += number * 12
  }

  mutating func sell(number: Int) {
    numberInStock -= number
  }
}
var lemonades = SoftDrinkItem(
  name: "lemonade",
  numberInStock: 24,
  isFizzy: true)
var stocks: [StockItemProtocol] = [mangos, lemonades]

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.

// 1
extension StockItemProtocol {
  // 2
  mutating func buy(number: Int) {
    numberInStock += number
  }

  mutating func sell(number: Int) {
    numberInStock -= number
  }
}
mangos.numberInStock = 0
mangos.buy(number: 2)
mangos.sell(number: 1)

lemonades.numberInStock = 0
lemonades.buy(number: 1)
lemonades.sell(number: 1)
Protocol methods
Hbatixir tenkutw

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