Today I tried playing bit with OptionSet in Playground, and I notice this pattern
struct Activities: OptionSet {
let rawValue: Int
static let eating = Activities(rawValue: 1)
static let programming = Activities(rawValue: 2)
static let breathing = Activities(rawValue: 3)
static let saveGotham = Activities(rawValue: 4)
}
let act: Activities = [.eating, .programming, .saveGotham]
act.contains(.breathing). //true /* this is unexpected */
act.contains(.saveGotham) //true
Although, the array doesn't contain the value '.breathing' it still return true. I modified the same struct with different rawValue
struct Activities: OptionSet {
let rawValue: Int
static let eating = Activities(rawValue: 1)
static let programming = Activities(rawValue: 8)
static let breathing = Activities(rawValue: 16)
static let saveGotham = Activities(rawValue: 32)
}
let act: Activities = [.eating, .programming, .saveGotham]
act.contains(.breathing). //false
act.contains(.saveGotham) //true
and got the desired output. it would be awesome if someone shed the light on the problem and explain how the 'OptionSet' actually works.
Thank you.
The OptionSet protocol is meant to
In your case,
is stored as an integer containing the BITWISE OR of the raw values (
1 | 2 | 4 = 7), andtests if the BITWISE AND
7 & 3is non-zero (which is the case).Therefore you should not use consecutive raw values, but powers of two, i.e. each of the mutually exclusive values is represented by one bit position:
or equivalently:
Now everything works as expected: