Chapters

Hide chapters

UIKit Apprentice

Third Edition · iOS 18 · Swift 5.10 · Xcode 16

My Locations

Section 3: 11 chapters
Show chapters Hide chapters

Store Search

Section 4: 13 chapters
Show chapters Hide chapters

21. Swift Review
Written by Fahim Farook

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

You have made great progress! You’ve learnt the basics of Swift programming and created two applications from scratch. You are on the threshold of creating your next app.

But, a good building needs a good foundation. And in order to strengthen the foundations of your Swift knowledge, you first need some additional theory. There is still a lot more to learn about Swift and object-oriented programming!

In the previous chapters I’ve shown you a fair bit of the Swift programming language already, but not quite everything. Previously, it was good enough if you could more-or-less follow along with what we were doing, but now is the time to fill in the gaps in theory. So, let’s do a little refresher on what we’ve talked about so far.

In this chapter, you will cover the following:

  • Variables, constants, and types: the difference between variables and constants, and what a type is.
  • Methods and functions: what are methods and functions — are they the same thing?
  • Making decisions: an explanation of the various programming constructs that can be used in the decision making process for your programs.
  • Loops: how do you loop through a list of items?
  • Objects: all you ever wanted to know about Objects — what they are, their component parts, how to use them, and how not to abuse them.
  • Protocols: the nitty, gritty details about protocols.

Variables, constants and types

Variables and types

A variable is a temporary container for a specific type of value:

Constants

Variables are not the only code elements that can hold values. A variable is a container for a value that is allowed to change over the course of the app being run.

Value types vs. reference types

When working with basic values such as integers and strings — which are value types — a constant created with let cannot be changed once it has been given a value:

Collections

A variable stores only a single value. To keep track of multiple objects, you can use a collection object. Naturally, I’m talking about arrays (Array) and dictionaries (Dictionary), both of which you’ve seen previously.

Generics

Array and Dictionary are known as generics, meaning that they are independent of the type of thing you want to store inside these collections.

Optionals

Sometimes it’s useful to have a variable that can have no value, in which case you need to declare it as an optional:

Methods and functions

You’ve learned that objects, the basic building blocks of all apps, have both data and functionality. Instance variables and constants provide the data, methods provide the functionality.

Parameters

Often methods have one or more parameters, so they can work with multiple data items. A method that is limited to a fixed set of data is not very useful or reusable. Consider sumValuesFromArray(), a method that has no parameters:

Making decisions

The if statement looks like this:

Comparison operators

You use comparison operators to perform comparisons between two values:

Logical Operators

You can use logical operators to combine two expressions:

switch statement

Swift has another very powerful construct in the language for making decisions, the switch statement:

return statement

Note that if and return can be used to return early from a method:

Loops

You’ve seen the for...in statement for looping through an array:

Looping through number ranges

Some languages have a for statement that looks like this:

while statement

The for statement is not the only way to perform loops. Another very useful looping construct is the while statement:

Objects

Objects are what it’s all about. They combine data with functionality into coherent, reusable units — that is, if you write them properly! The data is made up of the object’s instance variables and constants. We often refer to these as the object’s properties. The functionality is provided by the object’s methods.

Properties

There are two types of properties:

Methods

There are three kinds of methods:

Protocols

Besides objects, you can also define protocols. A protocol is simply a list of method names and possibly, properties:

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