Member-only story
How to correctly model data in Swift
Swift: Enumerations
Use cases and Implementation
In this article, we’re going to learn about enumerations
in Swift or as they’re more commonly known, enums.
An enumeration defines a common type for a group of related values and enables you to work with those values in a type-safe way within your code — swift.org
Enumerations
are used to model a finite dataset and found commonly in different programming languages. In Swift its history can be traced back to C language but enumerations
in Swift are a lot more flexible.
Why Enumerations: the problem
Swift offers multiple ways to model data to be used in a program. We can use object and collections to do that. But there is certain kinds of data where normal objects
and collections
don’t quite suffice the need.
Enumerations
are extensively used to represent fixed set of values.
But what is the issue with normal data types? Let’s understand with help of the following example: