DEV Community

Lou Franco
Lou Franco

Posted on • Edited on • Originally published at app-o-mat.com

The Swift Programming Language Companion: Structures and Classes

This article is part of a series on learning Swift by writing code to The Swift Programming Language book from Apple. This is the third article in Section 2 (here is Section 1)

Read each article after you have read the corresponding chapter in the book. This article is a companion to Structures and Classes.

Set up a reading environment

If you are jumping around these articles, make sure you read the Introduction to see my recommendation for setting up a reading environment.

To add a new page, in Xcode:

  1. Choose File > New > Playground Page
  2. Rename the page to "09-Structs-Classes"

Exercises for Structures and Classes

At this point, you should have read Structures and Classes in The Swift Programming Language. You should have a Playground page for this chapter with code in it that you generated while reading the book.

Review

This chapter is not so much about how to make Structures and Classes, but more about the difference between them. They both have properties and methods (as we'll see in the next two chapters), and are sometimes interchangeable. To beginners, it can be confusing to tell the difference between them or decide when to use one over the other.

Structure instances are values. We have already seen many other values, like Int, String, and enumerations. Values can never change. 5 is a value, and it makes no sense to change 5 to 6 -- I can add the value 1 to the value 5 and get a new value 6, but I can't change 5. I can't change "Hello" and I can't change June 8, 2020.

If I have a variable that is set to a value, I can change which value it is set to, but I cannot change the value itself. For example:

var a = 5
var b = a
a += 1 // b is still 5 -- a refers to a new value
Enter fullscreen mode Exit fullscreen mode

Class instances are objects with identity. It is possible to change them, and individual objects can be told apart (whereas every equal value is completely indistinguishable). If multiple variables refer to the same object, and we change it, we can see that change using any variable that refers to that same object.

Even if two objects are equivalent, they might not be identical. If I copy an object to a new one, the copy will not change if I change the original one.

You will learn when to use one or the other through experience. To start with, try to favor structs over classes.

Exercises

The chapter covers how to declare Structures and Classes and the differences between them.

For these exercises, we are going to imagine a simple media playing app.

In your Playground write code to do the following:

  1. Make a struct that represents a song (name it SongStruct). Songs have a title, an artist, and a releaseDate.
  2. Make an array of SongStruct values.
  3. Make a class that represents a song (name it SongClass). Add the same three properties
  4. Make an array of SongClass objects.
  5. Write code to explore the difference between them.
    1. Change the properties in the two types from let to var
    2. Make variables that are set to instances of each and change the properties
    3. Do the same thing, but set other variables to the ones you make (var b = a) first and see if changing a changes b.
    4. Try to change the instances inside the arrays

Next

The next article will provide exercises for the Properties chapter.

Top comments (0)