例如,在EventKit中,EKCalendar上有一个名为supportedEventAvailabilities的属性,它的类型为EKCalendarEventAvailabilityMask.它是所有支持的事件可用性的掩码.我知道你理论上可以像这样设置它们(如果这个
calendar.supportedEventAvailabilities = [.Busy, .Free]
但是你怎么再读这些价值呢?我最终得到了这样的代码:
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! }
但这感觉不对.有谁知道如何正确地做到这一点?
EKCalendarEventAvailabilityMask符合新的OptionSetType从SetAlgebraType继承并提供类似集合的接口的协议.您可以查看会员资格
let availabilities = calendar.supportedEventAvailabilities if availabilities.contains(.Busy) { // 'Busy' is in there! }
要么
if availabilities.isSupersetOf([.Busy, .Free]) { // Both .Busy and .Free are available ... }
要么
if availabilities.intersect([.Busy, .Free]) != [] { // At least one of .Busy and .Free is available ... }
有关更多信息,请参阅protocol SetAlgebraType
在“Swift标准库参考”中.