Swift Programming Tutorial: Enums

Arc Sosangyo
2 min readSep 6, 2021
Photo by: Sigmund at unsplash.com

This article is part of my Swift Programming Tutorial series.

The primary goal of declaring a data type is to avoid unnecessary related errors in your program by making you code clean and organized.

Swift has 6 basic data types: string, character, integer, float, double, and bool. This data types are built in order for Swift to store different kinds of values in both variables and constants. Then there’s enums, shorthand for enumerations, that allows you to customize your data type and define the values inside.

To appreciate the usefulness of customizing data type with enums, let’s compare using a switch statement without and with enums.

Below is a switch statement without relying on enums.

E.g.

var fellowRace: String = "Aragorn" switch fellowRace {
case "Aragorn": print("Human")
case "Legolas": print("Elf")
case "Gimli": print("Dwarf")
case "Frodo": print("Hobbit")
default: print("N/A")
}

The output will be:

Human

The above code obviously works fine. But there will be situations that you might misspell “Aragorn” to “Aragon”. Mistyping your code is not good in real situation if you’re writing a sophisticated program.

--

--

Arc Sosangyo

Arc is an iOS Dev and app publisher, a former IT manager who transitioned to iOS engineering, and a big fan of AI, coding, science, history, and philosophy.