OptionSet usage in Swift like NS_OPTIONS

Swift 3.0 introduced the OptionSet protocol after renaming from OptionSetType. It is a Swift alternative to the well-known and often used NS_OPTIONS in Objective-C. Swift imports NS_OPTIONS types as conforming to the OptionSet protocol. It presents a set-like interface for options:

struct CoffeeManipulators : OptionSet {
    let rawValue: Int

    static let milk     = CoffeeManipulators(rawValue: 1 << 0)
    static let sugar    = CoffeeManipulators(rawValue: 1 << 1)
    static let milkAndSugar = [milk, sugar]

The contains method is used to check whether an option is used. This makes it easy to add accessor methods like in the following example the hasMilk and hasManipulators methods are.

struct Coffee {
    let manipulators: [CoffeeManipulators]

    // You can now simply check if an option is used with contains
    func hasMilk() -> Bool {
        return manipulators.contains(.milk)

    func hasManipulators() -> Bool {
        return manipulators.count != 0

This can be handy if you defined a certain class or view which is able to set-up with different options.