The use of bitwise operations to hold multiple options in one value is a common pattern, and is used throughout the Cocoa Touch framework, with one example being UIDeviceOrientation. In Swift, there is a protocol, OptionSet, that formalizes this pattern and provides additional convenience.
Option set
How to do it...
Let's rewrite our supported device values to be an OptionSet:
struct Devices: OptionSet {
let rawValue: Int
static let phone = Devices(rawValue: 1 << 0)
static let tablet = Devices(rawValue: 1 << 1)
static let watch = Devices(rawValue: 1 << 2)
static let laptop = Devices(rawValue: 1 << 3)
static let desktop = Devices(rawValue...