Ethan Ethan - 1 year ago 201
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 Source

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 {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download