Member-only story

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

Types are first-class citizens. Right? What?

Steven Curtis
3 min readDec 16, 2019

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 hole in the existing tutorials.

--

--

No responses yet