Q:

How to handle masks (enums) in Swift 2.0?

For example in EventKit there is a property on EKCalendar called supportedEventAvailabilities which is of type EKCalendarEventAvailabilityMask. It is a mask of all the supported event availabilities. I know you could theoretically set them like this (if this property was writable):
calendar.supportedEventAvailabilities = [.Busy, .Free]

But how do you read those values again? I ended up with code like this:
let availabilities = calendar.supportedEventAvailabilities
if (availabilities.rawValue & EKCalendarEventAvailabilityMask.Busy.rawValue) == EKCalendarEventAvailabilityMask.Busy.rawValue {
    // 'Busy' is in there!
}
if (availabilities.rawValue & EKCalendarEventAvailabilityMask.Free.rawValue) == EKCalendarEventAvailabilityMask.Free.rawValue {
    // 'Free' is in there!
}

But this does not feel right. Does anyone know how to do this properly?

A:

EKCalendarEventAvailabilityMask conforms to the new OptionSetType
protocol  which inherits from SetAlgebraType and offers a set-like interface. You can check for membership with
let availabilities = calendar.supportedEventAvailabilities
if availabilities.contains(.Busy) {
    // 'Busy' is in there!
}

or
if availabilities.isSupersetOf([.Busy, .Free]) {
    // Both .Busy and .Free are available ...
}

or 
if availabilities.intersect([.Busy, .Free]) != [] {
    // At least one of .Busy and .Free is available ...
}

For more information, see protocol SetAlgebraType
in the "Swift Standard Library Reference".

