【问题标题】:Get the Calendar Color from EKEvent in Swift在 Swift 中从 EKEvent 获取日历颜色
【发布时间】:2020-11-30 19:10:37
【问题描述】:

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

【问题讨论】:

    标签: swift colors eventkit ekevent ekcalendar


    【解决方案1】:

    看起来EKEventEKCalendarItem 的子类。 EKCalendarItem 包含一个属性 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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-13
      • 1970-01-01
      • 1970-01-01
      • 2017-07-29
      • 1970-01-01
      • 2012-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多