Option Sets

A portrait painting style image of a pirate holding an iPhone.

by The Captain

on
May 18, 2023

Understanding Swift Option Sets

Swift is a modern programming language that has made significant advancements in simplifying coding complexities. One of the most powerful features of Swift is its Option Sets. Option Sets are arrays of options that allow for easy bitwise operations. They are ideal for handling data that has multiple states, without requiring you to use several variables. Option Sets simplify coding by enabling you to use bitwise operations on enums. In this tutorial, we will explore how to create and manage option sets in Swift. Let's begin by creating an example to demonstrate how to use option sets. Suppose you are building an application that offers users the option to select their favorite fruits from a list. You could use an option set to indicate which fruits have been selected. The first step would be to create a new option set. In the code snippet below, we create an option set to represent different types of fruits:
struct FruitsOptionSet: OptionSet {
    let rawValue: Int
    
    static let apple = FruitsOptionSet(rawValue: 1 << 0)
    static let orange = FruitsOptionSet(rawValue: 1 << 1)
    static let banana = FruitsOptionSet(rawValue: 1 << 2)
}
In the above code, we declare a new Option Set called 'FruitsOptionSet.' The 'rawValue' is the integer representation of the option set. We then define three options within the option set—apple, orange, and banana. Now that we have our option set defined, we can use the OptionSet methods to add or remove options. Here is an example of adding items to our newly created option set:
var fruits: FruitsOptionSet = []
fruits.insert(.apple)
fruits.insert(.banana)}
Notice that we used 'insert' to add the apple and the banana to our fruits option set. We can also use remove method to remove options:
fruits.remove(.banana)}
We can check whether an option is contained within the option set:
if fruits.contains(.apple) {
    print("Apple is selected")
} else {
    print("Apple is not selected")
}
We can also use bitwise operations on enums to check whether two option sets have common options. Here's how we might use bitwise operations to check whether the user has selected a common fruit:
let selectedFruits: FruitsOptionSet = [.apple, .banana]
let availableFruits: FruitsOptionSet = [.apple, .orange, .banana]

if !selectedFruits.intersection(availableFruits).isEmpty {
    print("User has selected at least one available fruit!")
} else {
    print("User has not selected any of the available fruits.")
}
In the code above, we used 'intersection' to check whether the intersection of available fruits and the user-selected fruits is empty. If it is not empty, we have a match. In conclusion, option sets are a powerful Swift feature that can simplify the process of managing multiple states in code. They are ideal for handling data that has multiple states without requiring several variables. They can be created using the OptionSet protocol and can take advantage of its many features, such as insert, remove, and bitwise operations.