The article explains the use of OptionSet in Swift, a protocol for representing bitset types, and compares it to traditional enumerations for managing groups of related options.
Abstract
In the article titled "What is OptionSet in Swift and When You Should Use It?", the author delves into the concept of OptionSet in Swift programming, highlighting its advantages over regular enumerations for handling a collection of related members. The author illustrates this with a practical example of developing a project management tool where tasks have varying levels of importance and difficulty. The standard approach using an enumeration is shown to be limiting when tasks need to have multiple attributes. The author then transitions to using OptionSet, demonstrating its ability to handle multiple options elegantly, the simplicity of its implementation, and the powerful set-like operations it supports, such as intersection and union. The article concludes with a recommendation to refer to Apple's official documentation for a deeper understanding of OptionSet.
Opinions
The author suggests that using a standard enumeration for multiple attributes in a property becomes complex and verbose.
The use of an array to store multiple enumeration values is seen as an inelegant solution that requires additional steps to ensure uniqueness.
The OptionSet protocol is presented as a superior alternative to enumerations for representing and working with a group of options in a type-safe manner.
The author emphasizes the ease of use and clarity provided by OptionSet when dealing with multiple properties, as well as its built-in capability to perform set operations without manual implementation.
The article conveys that OptionSet simplifies the codebase, making it more readable and maintainable for Swift developers.
The author's choice to use OptionSet for the example task management system implies a preference for this approach in similar real-world scenarios.
What is OptionSet in Swift and When You Should Use It?
In this brief tutorial, we will learn what OptionSet in Swift is and when we might want to use it in our iOS apps.
We use OptionSet to represent and work in a type-safe way with a group of related members (or options). Its concept is similar to the enum, but it has some quite useful features that are not available in mere enumerations.
Let’s Start
Say we’re developing a project management tool and want to have tasks of different importance and difficulty. The standard way to do this is with an enumeration:
To instantiate a TaskType, we write the following:
But what if we want our Item to be both not urgent and easy? We have to make the taskType property an array and change its signature to taskTypes:
Now, to check if the item is both not urgent and easy, we write this relatively long if statement:
We also want the taskTypes array to contain only unique members (no two .notUrgents allowed). So we transform this array into a Set:
That’s too many steps for a simple thing? So we have a better alternative — OptionSet.
OptionSet Implementation
Let’s perform the tasks above but with an OptionSet instead of an enum. First, we should create the TaskTypes structure:
When we conform to the OptionSet protocol, it’s required that we provide a rawValue for the structure. According to Apple’s documentation, we can use types that conform to the FixedWidthInteger, such as Int, or UInt8.
If we look at the urgent property, we see the following pattern: 1 << 0. The left value represents an integer multiplied by two in the power of the value on the right side. To clarify, one should be multiplied by two in the power of zero: 1 * 2⁰ = 1.
Accordingly, the rawValue for the notUrgent is 1 << 1. So one is being multiplied by two in the power of one: 1 * 2¹ = 2. Because of this bit operation, we have a unique value for each member of a set.
With that being clear, let’s move to the next part — instantiating and using the created OptionSet.
Use OptionSet
Let’s define an Item and instantiate it:
The code is now more concise, but with OptionSet we’ve gained some other benefits:
We can now perform Set-related operations, like intersection:
Or union:
This code prints the value of 9, because .urgent corresponds to 1, while .hard corresponds to 8. Their sum is 9, which is unique and can be obtained only when performing union with .urgent and .hard.
And many other handy operations:
Wrapping Up
To learn more about OptionSet, refer to the official Apple documentation: