UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How to create a custom OptionSet

Swift version: 5.6

Paul Hudson    @twostraws   

Option sets are similar to enums, but they are designed to work as a set so you can use more than one at a time. For example, when using the range(of:) method of a string, you can specify .caseInsensitive to have the search ignore case, you can specify .backwards to have the search start from the end of the string, or you can combine the two:

let string = "The rain in Spain"
let range = string.range(of: "rain", options: [.caseInsensitive, .backwards])

That searches through the string backwards and ignoring case – we provided both options at the same time. This functionality looks like an enum, but it can also be treated as an array; Swift figures it out for you.

You can write your own by making a custom struct that conforms to the OptionSet protocol, and it doesn’t take much:

  1. Create a constant describing what the underlying value is – it’s normally an integer, but you need to be specific.
  2. Make static instances of your struct for each option you want to represent.
  3. Each of those should have a unique raw value, so its common to use bit-shifting to avoid mistakes.
  4. Add any groups of those instances as new statics.

To demonstrate this, let’s create a UserRoles struct that defines roles a user might have in a GitHub account: they can create things, destroy things, and get the status of things.

All three of those roles need unique raw values, so we’re going to use bit shifting – 1 << 0, 1 << 1, and so on – to make that clear.

Here’s how it looks in Swift:

struct UserRoles: OptionSet {
    let rawValue: Int

    static let status = UserRoles(rawValue: 1 << 0)
    static let create = UserRoles(rawValue: 1 << 1)
    static let destroy = UserRoles(rawValue: 1 << 2)
    static let all: UserRoles = [.status, .create, .destroy]

You can now use any of those roles by themselves or in an array:

let roles1: UserRoles = [.create]
let roles2: UserRoles = [.create, .destroy]
let roles3: UserRoles = [.create, .destroy, .status]
let roles4 = UserRoles.all

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Available from iOS 8.0

Similar solutions…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.4/5

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.