Bitwise Enum Values with the OptionSet in Swift

Batikan Sosun
Sahibinden Technology
3 min readApr 13, 2022

--

Photo by Joshua Sortino on Unsplash

In this article, I will give my best approach to explain the benefit of using OptionSet in your code design and show some great methods to define them.

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 doc.

In Swift language, the enumerations are one of the most powerful type-related values. The enumeration cases can specify associated values of any type to be stored along with each different case value.

Enumeration Syntax

You see a basic definition of enumeration in the below example. Enumeration declaration starts with the “enum” key.

enum Direction {case leftcase rightcase bottomcase top}

That was the basis of enumeration, as you understand in the above definition.

In most cases, you need to define a single variable that has to store many potential values. That sounds a little strange.
So, Imagine that, you have a status that can be in several states at the same time. Do not look for the answer elsewhere. In this case, bitwise enum, so OptionSet saves you.

What is OptionSet Type?

Essentially, OptionSet is a Protocol that conforms to make you able to do bit masking(C style). OptionSet represents a bitset type, and every single bit represents an option.

OptionSets are very similar to enums.

Using an OptionSet Type

struct Direction: OptionSet {let rawValue: Intstatic let left = Direction(rawValue: 1 << 0)static let right = Direction(rawValue: 1 << 1)static let top = Direction(rawValue: 1 << 2)static let bottom = Direction(rawValue: 1 << 3)static let all: Direction = [.left, .right, .top, .bottom]}

In the above example, we conformed OptionSet protocol to the Direction enum. At the top of the content of the Direction enum, we placed an immutable variable that’s called rawValue. OptionSet requires a rawValue for defining each enum value that contains a bit.

Let’s take a look at the usage of the Direction example and what does it provide us?

func printDirections(direction:Direction) {if direction.contains(.left) {print(“left”)}if direction.contains(.right) {print(“right”)}if direction.contains(.top) {print(“top”)}if direction.contains(.bottom) {print(“bottom”)}}

I defined a function named “printDirections”.
That function takes an argument that is type-constrained by Direction.
It looks like pretty clear code.

printDirections(direction: .all)
printDirections(direction: .right)

When you call the function named “printDirections” that will print out the below values.

left
right
top
bottom
right

Another declaration is below.

let direction: Direction = [.all]

On the other hand, many times you have seen that cases in Swift or Objective-C
Such a below example.

view.autoresizingMask = .flexibleHeight
view.autoresizingMask = [.flexibleHeight, .flexibleWidth]

Wrapping Up

OptionSet is a great way to deal with multipotential types without the need to define an array of types.
Usage of the OptionSet provides clear syntax and more readable code. I guess this piece of the article should provide you with a very good understanding of OptionSet.

Reference:

Swift Documentation: https://developer.apple.com/documentation/swift/optionset

Thanks for Reading!

Let’s connect on Twitter

--

--

Batikan Sosun
Sahibinden Technology

Tweeting tips and tricks about #swift #xcode #apple Twitter @batikansosun Weekly Swift Blogging