How to handle masks (enumerations) in Swift 2.0?

For example, in EventKit there is a property on EKCalendar, called supportedEventAvailabilities, which has a type EKCalendarEventAvailabilityMask. This is the mask of all supported events. I know that you could theoretically set them like this (if this property was writable):

calendar.supportedEventAvailabilities = [.Busy, .Free]

But how do you read these values ​​again? I got 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 is not so. Does anyone know how to do this properly?

+4
source share
1 answer

EKCalendarEventAvailabilityMaskcorresponds to a new OptionSetType protocol that inherits from SetAlgebraTypeand offers a dial-up interface. You can check membership in

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 ...
}

. protocol SetAlgebraType " Swift".

+3

All Articles