This content originally appeared on Level Up Coding – Medium and was authored by Itsuki
What is an OptionSet?
It is a protocol that presents a mathematical set interface to a bit set.
Honestly speaking, I use the structs conforming to this protocol all the time (because Swift uses a fair amount in those frameworks), but probably don’t creating much of it from scratch by myself!
It deserves more!
Since the OptionSet conforms to SetAlgebra, it really makes set-related operations such as membership tests, unions, and intersections EASY!
If you have any experience on working with OptionSet, one of the common questions would be why to use it rather than enum?
Here is MY rule of thumb!
If I am using enum as a Set of the cases, I will use OptionSet instead!
**************
In this article, let’s start with a quick example on the system-provided OptionSet to get a taste of it!
We will then move onto creating our own type conforming to the protocol and some of the main operations we can perform on it!
Quick Example
If you like SwiftUI like me, one of the ones we probably use all the time is that EditActions when creating List with ForEach.
// EditActions: https://developer.apple.com/documentation/swiftui/editactions
let editActionDeleteMove: EditActions<[String]> = [.delete, .move] // rawValue: 3
let editActionDelete: EditActions<[String]> = .delete // rawValue: 1
let editActionMove: EditActions<[String]> = .move // rawValue: 2
let editActionAll: EditActions<[String]> = .all // rawValue: 3
assert(editActionAll == editActionDeleteMove)
Since the OptionSet represents bit set types, the rawValue of the combination will be the same as the sum of the individual rawValues!
And this is what makes OptionSet powerful!
Create Our Own OptionSet
Let’s say we have a bunch of TodoTags that we want the user to be able to select from and attach to a Todo item.
Since the same tag cannot be attached twice, and a Todo item can contain 0 or more tags, here would be a scenario where OptionSet shines!
struct TodoTags: OptionSet {
let rawValue: UInt
static let favorite = TodoTags(rawValue: 1 << 0) // 1
static let urgent = TodoTags(rawValue: 1 << 1) // 2
static let work = TodoTags(rawValue: 1 << 2) // 4
static let all: TodoTags = [.favorite, .urgent, .work]
static let urgentWork: TodoTags = [.urgent, .work]
}Couple Little points here!
First of all, we have to include the rawValue property to conform to the protocol. If we want to automatically receive default implementations for set-related operations, it has to be a type conforms to the FixedWidthInteger protocol such as Int or UInt8.
Now, why do we use this bitwise left shift operator (<<) instead of just some raw numbers?
I meant, You COULD, but it is super easy to mess up, and I really cannot recommend it!
And here is why!

As you can see 1 << 2 is 4, not 3!
Obviously, but at same time, hard to realize if we are using the numbers directly!
Set Operations
As I have mentioned above, OptionSet conforms to SetAlgebra so we can perform operations such as intersection, union, or subtraction super efficiently!
Here is a quick snippet of what we can do!
assert(TodoTags.all.intersection(.urgent) == .urgent)
assert(TodoTags.all.intersection(TodoTags.urgentWork) == [.urgent, .work])
assert(TodoTags.urgent.union(.work) == [.urgent, .work])
assert(TodoTags.urgent.union(.all) == [.urgent, .work, .favorite])
assert(TodoTags.all.contains(.work))
assert(TodoTags.urgentWork.isSubset(of: .all))
assert(TodoTags.urgentWork.isSubset(of: .urgentWork) == true)
assert(TodoTags.urgentWork.isStrictSubset(of: .urgentWork) == false)
assert(TodoTags.all.isSuperset(of: .urgentWork))
assert(TodoTags.urgentWork.isSuperset(of: .urgentWork) == true)
assert(TodoTags.urgentWork.isStrictSuperset(of: .urgentWork) == false)
Reference
- Advanced Operators: If you need more details on any of the bitwise / bit-shift operators, want to provide your own implementations of existing operators, this Swift official Documentation is literarily the BEST! Graphic examples, operator methods, you name it!
- Set Algebra
- OptionSet
Thank you for reading!
That’s it for this article!
Happy option-setting!
Swift: A Little Dive Into OptionSet was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding – Medium and was authored by Itsuki