Classes, Enums or Structures? How to choose your Swift Type

Types are first-class citizens. Right? What?

Choosing which one of a Class, Enum or Struct can be tricky at the best of times. This tutorial is designed to help you understand the difference between these

Difficulty: Beginner | Easy | Normal | Challenging

Prerequisites:

  • Be able to produce a “Hello, World!” iOS application (guide HERE)
  • Some experience of Inheritance and Protocols in Swift to fully understand the examples

Terminology

class: An object defined in Swift, using pass by reference semantics

enum (enumeration): A type consisting of a set of named values, called members.

struct (Structure): An object defined in Swift, using pass by value semantics

Why?

On the surface Classes and Structs are similar, they both store data in variables and are flexible data types. Then Enums come in as a set of named values and…perhaps it is a little bit confusing because we can combine Enums with Classes and Structs. The shared use of variables with the lack of examples has left a whole in the existing tutorials.

Shall we press on?

Classes and Structs?

You can use Classes or Structs to use the variables(and the full tutorial on the theoretical difference is HERE).

However, here in the real world when you create a new project in Xcode (tutorial HERE) the view controller is a class and that means it is a reference type.

A single case for a class

Imagine you wish to create a view controller, and pass the reference to a popup that appears in front of your view controller. In fact, the demo repo shows how this might be done (although it doesn’t look great, admittedly) — but the point is that this should work for any host view controller, and we want it to change the referenced instance of the view controller.

Oh yes, you do clearly want to use a class for view controllers.

Image for post
Image for post

Now classes can also have extensions and other features HERE.

Classes we define often look something like the following:

class Person {
var age : Int
init (age: Int) {
self.age = age
}
}

A single case for a struct

A single integer is a value type represented by a struct (as defined by Apple, HERE). This means predicable behaviour like that below:

var myNum = 5
var mySecondNum = myNum
myNum = 6
print (myNum, mySecondNum) // 6, 5

When we copy the value we make a, well, copy of it. Good stuff.

A common struct you might create might look like the following:

struct Rectangle {
var height : Float
var width : Float
}

Enums

However enums are used as a type to manage a group of related values. The advantage is that the way you can manipulate these values is type-safe.

You may commonly see the following for an error type

enum MediumErrors: Error {
case tooBoring
var tooLong
}

However Enumerations in Swift are flexible, and don’t even have to provide values for each case of the enumeration. Now we are getting into this, we need some real-world examples.

Syntax

Enumerations are defined with the enum keyword and have values stated.

A great example for this is to use enumerations to represent responses to a survey.

Let us take this easy: for a simple question you might only have the options yes or no for many of the questions in our survey.

Conclusion…

Enums give ways to represent collections of values that avoid String matching, in a way that Classes and Structs simply can’t.

It often feels that Enums shouldn’t be pushed into the same tutorials as Classes and Structs, but they often are and knowing the difference and how to use them is in fact a great powerful piece of knowledge that it is worth time to develop.

Want to get in contact? Try the link here:

https://twitter.com/stevenpcurtis

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store