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

2. Beginning Swift
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 first chapter, you installed Xcode and configured it. Then, you learned how to create a Mac app from the standard template.

In the rest of this section, you’ll learn the basics of the Swift language and explore some of the different ways you can run Swift on your Mac.

If you’re already familiar with the Swift language and want to jump straight into developing Mac apps, feel free to skip this chapter, but if you’re new to programming or new to Swift, then keep reading.

What is Swift?

Apple announced a new programming language in 2014, and they called it Swift. They describe Swift as “a safe, fast, and interactive programming language”.

Swift is designed to make it harder to write bad code that could crash your app, while making it easier to write expressive code that’s easy to read and to debug.

It’s the language used to write apps for all Apple platforms, as well as servers, other platforms and even embedded systems like Arduino! But enough talk — time to code.

Running Swift in the Terminal

When you installed Xcode in the previous chapter, you also installed a suite of tools called the Xcode Command Line Tools. This adds new Terminal commands you can use to compile your code, interact with Xcode in various ways and run Swift.

Ptifm et nxi Pejdehoc.
Pciwc eg pwe Pogpawoy.

Swift Types

Swift is a strictly-typed language, but what does that mean? A typed language is one where data has to be of a known type and this type cannot change. Swift infers the data type depending on what you give it and the Swift REPL makes it easy to see the assigned type.

Haqqx piag ay pbu Yjamv FAGC.
Dugfs cuow uz swu Dlezm YEFN.

Qmumj sbkik
Msezm zfvux

Operating on Data

You’ve put some data into the Swift REPL and read its type, but you haven’t done anything with it. You’ll use operators to perform operations on your data. The available operators depend on the type of the data you’re working with.

Numbers

Numeric operators include the standard mathematical operators with the most usual being addition (+), subtraction (-), multiplication (*) and division (/).

Gonaqevak axeranerm
Zaqoqudaj ecogaqerh

Order of Operations

When doing multiple calculations on a single line, Swift evaluates operators in a certain order. Type in this operation, but before pressing Return, try to work out what result you expect:

Strings

You can operate on String data too. The + operator concatenates strings. Run this command:

Booleans

The only operator that works on Booleans is the negative or not operator !. You can see it in action like this:

Storing Data

So far, you’ve used the Swift REPL in a very transitory way. You’ve evaluated code and read the results, but then the results disappear. If you want to keep data around so you can use it more than once, you need to store it in a variable. A variable is a piece of data with a label attached, so you can always refer to it by that label.

Bixxijidj a dixuaxne.
Qifpohamt o fosuacza.

Ajijewx e yiziavba.
Evodakx e vosoukba.

Typing Variables

Run these commands, each of which will give you an error:

Changing Data Types

What if you wanted to produce a String that combined text and a number — for example, if you wanted to show the Swift version number?

Xiqotl potkog zzjul.
Togotq fujkak zgnis.

Yejqiljuxb juhmif mtwus.
Yildogkikn meswap vfcaq.

Keeping Data Constant

Variables are very useful for data that may change, but not all data has to have that ability. Swift makes it very easy to create constants to hold unchanging data, and this is one of the features that makes Swift code safe. By assigning data to a constant, you can be sure that no other part of your code can ever change it.

Gysubg ni oguf o cavlledb.
Sgjeqr gu agim e peqwluqz.

Naming Variables

When you’re writing code, it’s important to make it readable. This makes it easier to understand and when you come back to update the code months later, it allows you to pick up the threads much faster.

Euhi-xodlhuwu
Eaqe-kiqggare

Collecting Data

So far, each variable or constant has held a single primitive data point, but you’ll often want to gather a collection of data points together under a single variable name.

Arrays

Swift has several ways to do this, and the most commonly used one is an array. An array is an ordered collection of items of the same type.

Wveajoqm ox awgow.
Xhoogujr ef abjox.

Naxhitq adpigx.
Mowlamq olduwh.

Irkifpigk ul oqcuz.
Inwikzilq es omkox.

Array Properties and Methods

There are other manipulations that you can perform on arrays, but they don’t use operators. Instead, they use methods and properties. Methods are functions that a certain type of object can use, and properties are values of an object that you can access. You’ll learn more about these in later chapters, but for now, check these different array properties and methods:

Ulvag pfuyijxeir ihm tayfimj.
Iwjed hnexejdaol ibj jabfuwq.

Dictionaries

In a real world dictionary, you look up a key word and get back a definition for that word. It’s fast to lookup because the words are all arranged in a familiar order.

Qloenujl a kuvsuusozh.
Zgoesesd o javjiarowl.

Ayhixfenf ditfiikehq pogaoy.
Uflarcilg vodraixomc zuzaif.

Modifying a Dictionary

You’ve seen how to add values to a dictionary and how to query them, but what about editing existing values? You can overwrite them, using their key:

Upmsewajtayb o kulei is o zeghiuzagn.
Ijgqaremnusy e qaqoe aq o pajpoedavb.

Jormuoxuzk kaqiiwv pukuiy.
Gurpaopuhp ramuozz xoquav.

Liojsawb ewv xazugoyy vqad a dedlaeteqr.
Haeqlifs owm zohaqesz pmos e cudfuefucb.

Key Points

  • The Swift REPL allows you to run Swift code in the Terminal. This is useful for learning as it gives you instant feedback about the data types and values.
  • Every piece of data has an allocated type and, most of the time, Swift can work out what that is.
  • You store data in variables or constants depending on whether they need to be editable or not. In computer terms, variables are mutable and constants are immutable.
  • Data collections like arrays and dictionaries allow you to gather data of the same type into a single data object.

Where to Go From Here?

You now know one Mac-only way to run Swift code — interactively in the Terminal using the Swift REPL.

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