Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: masks as type properties

Tags:

macos

swift

cocoa

my problem. I'm working with a NSEvent, which happens to have a var modifierFlags of type NSEventModifierFlags.

I want to check whether the user had a modifier key pressed (Command), so basically I want to check if CommandKeyMask is on.

What's the best/right way to do this in Swift ?

like image 358
mdomans Avatar asked Jun 13 '14 09:06

mdomans


1 Answers

The following works in Xcode7-beta3, Swift 2

In Swift 2, bit field style enums like NSEventModifierFlags have been updated to conform to the OptionSetType protocol.

If you want to check if an option set contains a specific option, you no longer need to use bitwise & and a nil check. You can simply ask the option set if it contains a specific value in the same way that you would check if an array contained a value.

if theEvent.modifierFlags.contains(.CommandKeyMask) {
like image 187
Phil L. Avatar answered Oct 29 '22 09:10

Phil L.