How should bit fields be declared and used in Swift?
Declaring an enum like this does work, but trying to OR 2 values together fails to compile:
enum
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
:
let compontentKeys : Set = [.day, .month, .year]
Whereas the original NSCalendarUnit
was a bitwise enum. So both approaches are usable (thus the original response remains valid)
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(arrayLiteral: .A, .D)
An and check (options & .A
) could be defined as:
options.contains(.A)
Or for multiple "flags" could be:
options.isSupersetOf(Set(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.