Ethan Ethan - 4 months ago 54
Swift Question

Checking bitmask in Swift

Tried the code below, doesn't compile

if button.keyEquivalentModifierMask & NSEventModifierFlags.CommandKeyMask != 0 {
}


What's a proper way to check for bitmasks in Swift?

Answer

keyEquivalentModifierMask is a Int, whereas NSEventModifierFlags.CommandKeyMask is an enum-like with UInt raw value, so you have to read the rawValue out of it and convert keyEquivalentModifierMask to Uint:

if UInt(button.keyEquivalentModifierMask) & NSEventModifierFlags.CommandKeyMask.rawValue > 0 {
}