简体   繁体   English

从 Swift 中的 EKEvent 获取日历颜色

[英]Get the Calendar Color from EKEvent in Swift

In Apple's EventKit each Calendar can have a user-definable color, which can also be accessed on the EKCalendar instance as EKCalendar.color .在 Apple 的 EventKit 中,每个日历都可以有一个用户可定义的颜色,也可以在 EKCalendar 实例上以EKCalendar.color的形式访问。 How can that color be accessed from a single event (not a calendar)?如何从单个事件(而不是日历)访问该颜色? Is there any back-reference from an EKEvent instance to the EKCalendar the event belongs to?从 EKEvent 实例到事件所属的 EKCalendar 是否有任何反向引用?

As an example, I have a list of calendars and fetch events by start and end date.例如,我有一个日历列表,并按开始和结束日期获取事件。 In the resulting array of events it seems any information answering a question " from which calendar a single event is " got lost.在由此产生的事件数组中,似乎任何回答“单个事件来自哪个日历”的问题的信息都丢失了。

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

It looks like EKEvent is a subclass of EKCalendarItem .看起来EKEventEKCalendarItem的子类。 The EKCalendarItem contains a property, calendar . EKCalendarItem包含一个属性calendar

That being said, here's the answers to the questions话虽如此,这是问题的答案

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM