Extensive Swift Enums Guide
Hi everyone,
In this article, I’m gonna talk about Enums in Swift. At Plus Minus One we love using enums.
Enums are mostly used as a basic type defining structure by most of the developers, but actually, they can be used in a very wide range.
You can see official documentation here. As you can see Enums are much more capable than you think.
I will be presenting some of the use cases of Enums from our team below.
Enums can conform to protocols
Yes, enums can conform protocols. You can use Swift’s own protocols or custom protocols. By using protocols with Enums you can add more capabilities.
Sometimes we need to use similar but different object types in the same place. For this, we need to define a generic type for these objects. For example, we can append different FilterType
and SizeType
enums to the same [BaseSelectionType]
array and checking theirs selectionType
fields we can safely cast to the desired type.
Enums with Switch Cases
This is the most common usage of enums. Instead of if-clauses, we can use switch-case statements which make our codebase more readable.
A switch-case statement can be used inside the enum with self
keyword also. With the next topic, you’ll see the related example.
Enums with Computed Properties
We can not use stored properties with enums but we can use computed properties.
In this example by using a switch statement with self
keyword, we are getting a string
to display it on the screen for the related FilterType
.
let text = FilterType.colorDocument.getDisplayText
We can use methods for these kinds of operations also.
We can add multiple computed properties and methods to our enum cases. By using them we can customize our enums and get all the related info inside them.
Enums with associated values
Creating our custom endpoints is one of the use cases where we use enums a lot. As you can see with this example we are using all the things we have talked about above.
By giving .getInfo(perPage: 100)
to our method, we provide that request()
method will get the path
variable from enum with the associated value given.
You can not define associated values with raw values. This is because raw values are predefined uniquely and static but associated values are given while initialization, thus they are dynamic.
Enums with CaseIterable
By conforming CaseIterable we gain access for using allCases
property, in this way we do not need to deal with adding all of the enum cases to an array. We are getting all of the cases of our enum type instantly.
Enums with Raw Values & Initialization with Raw Values
Instead of associated values, we can define our Enum cases with Raw Values in an old fashioned way.
Also, we can create enums by initializing them with raw values.
Enums with Custom Initialization
We can create our enum types by using custom init
methods also. Let’s explain this with an example. Assume that we’re getting our filter types from the backend with integer ids. Also, our app supports multi-language.
Considering these cases, let’s define localization id of each case to their raw values and define a custom init method which takes an integer value and returns related enum case.
With this approach whenever we get FilterType
‘s from backend we can initialize them with our custom init
method. By using predefined raw values we can get localized strings easily.
These kinds of little tricks would make your codebase more readable and understandable.
I’ve shown some usage of Swift Enums, but they offer more than those. You can use them recursively, they can also have extensions, subscripts, etc.
To learn more about our development experiences you can look at our medium page. At Plus Minus One, we love to learn and share our experiences. We hope this article makes your life easier 🙏🏻