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 definedinvisibleas1 << 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.
Now with those changes (note the removal of
None), the constants have the correct values. And now the value 20 does in fact containinvisible(16).You do not need the
nonevalue because that's easily represented by the empty option set which is what you get with a raw value of 0.