I need to understand how to know if a bitmask value has an OptionSet value that comes from my API. My OptionSet is this:
struct RecordsObjectivesStates: OptionSet {
var rawValue: Int
init(rawValue: Int) {self.rawValue = rawValue}
static let None = RecordsObjectivesStates(rawValue: 1 << 0)
static let RecordRedeemed = RecordsObjectivesStates(rawValue: 1 << 1)
static let RewardUnavailable = RecordsObjectivesStates(rawValue: 1 << 2)
static let ObjectiveNotCompleted = RecordsObjectivesStates(rawValue: 1 << 4)
static let Obscured = RecordsObjectivesStates(rawValue: 1 << 8)
static let Invisible = RecordsObjectivesStates(rawValue: 1 << 16)
static let EntitlementUnowned = RecordsObjectivesStates(rawValue: 1 << 32)
static let CanEquipTitle = RecordsObjectivesStates(rawValue: 1 << 64)
}
I have a response value of "20" from API. That is ObjectiveNotCompleted+Invisible. I want to know if Invisible is in my "20" value (and is it, obviously).
I found a lot of explanations about hexadecimal values or binary values, but nothing about Int values. Can anybody help me?
You state that the value 20 clearly contains the value invisible
. But this is incorrect. You have defined invisible
as 1 << 16
. That is 65536. The value 20 certainly does not contain 65536.
So you first need to fix your OptionSet. You should also name the constants starting with lowercase letters.
struct RecordsObjectivesStates: OptionSet {
var rawValue: Int
init(rawValue: Int) {
self.rawValue = rawValue
}
static let recordRedeemed = RecordsObjectivesStates(rawValue: 1 << 0) // 1
static let rewardUnavailable = RecordsObjectivesStates(rawValue: 1 << 1) // 2
static let objectiveNotCompleted = RecordsObjectivesStates(rawValue: 1 << 2) // 4
static let obscured = RecordsObjectivesStates(rawValue: 1 << 3) // 8
static let invisible = RecordsObjectivesStates(rawValue: 1 << 4) // 16
static let entitlementUnowned = RecordsObjectivesStates(rawValue: 1 << 5) // 32
static let canEquipTitle = RecordsObjectivesStates(rawValue: 1 << 6) // 64
}
Now with those changes (note the removal of None
), the constants have the correct values. And now the value 20 does in fact contain invisible
(16).
let states = RecordsObjectivesStates(rawValue: 20)
if states.contains(.invisible) {
// do stuff
}
You do not need the none
value because that's easily represented by the empty option set which is what you get with a raw value of 0.