Declaring and using a bit field enum in Swift

They showed how to do this in one of the WWDC videos.

let combined = MyEnum.One.toRaw() | MyEnum.Four.toRaw()

Note that combined will be Int type and will actually get a compiler error if you specify let combined: MyEnum. That is because there is no enum value for 0x05 which is the result of the expression.


Updated for Swift 2/3

Since swift 2, a new solution has been added as "raw option set" (see: Documentation), which is essentially the same as my original response, but using structs that allow arbitrary values.

This is the original question rewritten as an OptionSet:

struct MyOptions: OptionSet
{
    let rawValue: UInt8
    
    static let One = MyOptions(rawValue: 0x01)
    static let Two = MyOptions(rawValue: 0x02)
    static let Four = MyOptions(rawValue: 0x04)
    static let Eight = MyOptions(rawValue: 0x08)
}

let m1 : MyOptions = .One

let combined : MyOptions = [MyOptions.One, MyOptions.Four]

Combining with new values can be done exactly as Set operations (thus the OptionSet part), .union, likewise:

m1.union(.Four).rawValue // Produces 5

Same as doing One | Four in its C-equivalent. As for One & Mask != 0, can be specified as a non-empty intersection

// Equivalent of A & B != 0
if !m1.intersection(combined).isEmpty
{
    // m1 belongs is in combined
}

Weirdly enough, most of the C-style bitwise enums have been converted to their OptionSet equivalent on Swift 3, but Calendar.Compontents does away with a Set<Enum>:

let compontentKeys : Set<Calendar.Component> = [.day, .month, .year]

Whereas the original NSCalendarUnit was a bitwise enum. So both approaches are usable (thus the original response remains valid)

Original Response

I think the best thing to do, is to simply avoid the bitmask syntax until the Swift devs figure out a better way.

Most of the times, the problem can be solved using an enum and and a Set

enum Options
{
    case A, B, C, D
}

var options = Set<Options>(arrayLiteral: .A, .D)

An and check (options & .A) could be defined as:

options.contains(.A)

Or for multiple "flags" could be:

options.isSupersetOf(Set<Options>(arrayLiteral: .A, .D))

Adding new flags (options |= .C):

options.insert(.C)

This also allows for using all the new stuff with enums: custom types, pattern matching with switch case, etc.

Of course, it doesn't have the efficiency of bitwise operations, nor it would be compatible with low level things (like sending bluetooth commands), but it's useful for UI elements that the overhead of the UI outweighs the cost of the Set operations.


You can build a struct that conforms to the RawOptionSet protocol, and you'll be able to use it like the built-in enum type but with bitmask functionality as well. The answer here shows how: Swift NS_OPTIONS-style bitmask enumerations.