在 Swift 中从 EKEvent 获取日历颜色

Get the Calendar Color from EKEvent in Swift

在 Apple 的 EventKit 中,每个日历都可以有一个用户可定义的颜色,也可以在 EKCalendar 实例上访问 EKCalendar.color。 如何从单个事件(而不是日历)访问该颜色?是否存在从 EKEvent 实例到事件所属的 EKCalendar 的任何反向引用?

例如,我有一个日历列表,并按开始和结束日期获取事件。在事件的结果数组中,似乎任何回答“单个事件来自哪个日历”问题的信息都丢失了。

import EventKit

let eventStore = EKEventStore()
let calendars: [EKCalendar] = getCalendars() // get some calendars here

let d = (start: Date(), end: Date().addingTimeInterval(3600*5)) // [now, now+5h]
let predicate = eventStore.predicateForEvents(withStart: d.start, end: d.end, calendars: calendars)

let events = eventStore.events(matching: predicate) // fetch the events

for event in events { // iterate over all events from calendars within [now, now+5h]
   // ?? how to get the color of the calendar of event? or
   // ?? how to get the EKCalendar instance event is from?
}

看起来像EKEvent is a subclass of EKCalendarItemEKCalendarItem 包含一个 属性, calendar.

话虽这么说,这里是问题的答案

...
for event in events { // iterate over all events from calendars within [now, now+5h]
   // ?? how to get the color of the calendar of 
   let color = event.calendar.color

   // ?? how to get the EKCalendar instance event is from?
   let calendar = event.calendar
}