Member-only story

Enums in Swift

Press the switch on or off

Steven Curtis
3 min readFeb 4, 2020

enums let you define a type for a group of related values, so you can work with those values without using something awful (like Strings) that would keep you awake at nights. enums can be thought of as structs with the ability to catagorize.

Prerequisites:

  • Be able to produce a “Hello, World!” iOS application (guide HERE)
  • It would be useful to be familiar with Properties in Swift (guide HERE)

Terminology

Associated Values: Extra data that can be stored with enums to represent complex data

enum: A type consisting of a set of named values, called members

Stored Property: A constant or variable that is stored as part of a class or a struct

Type: This is the kind (type!) of value that a variable or constant can store

Simple implementations

This is not meant to be an introductory guide as the Swift programming guide https://docs.swift.org/swift-book/LanguageGuide/Enumerations.html really covers most of the simple uses of enums. However, it makes sense to start from somewhere sensible so imagine you want a case where you can buy or sell stock.

--

--

No responses yet