菜鸟教程小白 发表于 2022-12-11 17:34:18

ios - 创建新的 EKCalendar iOS 的适当情况


                                            <p><p>我正在开发一个创建、更新和删除 <code>EKEvents</code> 的 iOS 应用程序。这可以通过将事件保存到 <code>EKEventStore.defaultCalendarForNewEvents</code> 轻松完成。在什么情况下我想为我自己的应用创建一个新的 <code>EKCalendar</code>,这需要什么功能?</p>

<p>我之所以问,是因为我目前正在尝试在 Swift 3.0 中创建一个日历,但它一直失败,这让我一开始就想知道新日历的目的是什么。</p>

<pre><code>fileprivate var eventStore = EKEventStore()
fileprivate var newCalendar : EKCalendar?

func createNewCalendar() {
    self.newCalendar = EKCalendar(for: .event, eventStore: self.eventStore)
    self.newCalendar!.title = &#34;newCal&#34;
    let sourcesInEventStore = self.eventStore.sources
    self.newCalendar!.source = eventStore.defaultCalendarForNewEvents.source

    let newCalIndex = sourcesInEventStore.index {$0.title == &#34;newCal&#34;}
    if newCalIndex == nil {
      do {
            try self.eventStore.saveCalendar(self.newCalendar!, commit: true)
            print(&#34;cal succesful&#34;)
      } catch {
            print(&#34;cal failed&#34;)
      }
    }
}
</code></pre>

<p>我知道我可以访问 <code>eventStore</code>,因为我可以提取事件并将它们保存到 <code>EKEventStore.defaultCalendarForNewEvents</code> 并使用现有日历更新它们。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您想要创建新日历的原因可能有很多。就个人而言,当我想将一组事件与已创建并绑定(bind)到默认事件的事件分开时,我会选择创建一个新日历。通过这种方式,当您认为不需要它们时,您还可以利用批量删除这些新创建的事件。只需删除日历,其所有事件也将被清除。 </p>

<p>顺便说一句,如果你不确定你想要的日历的 <code>source</code> <em>(iCloud,本地,可能绑定(bind)到某些邮件帐户等)</em>要创建,只需使用默认的来源:</p>

<pre><code>let newCalendar = EKCalendar(forEntityType: .Event, eventStore: eventStore)
newCalendar.source = eventStore.defaultCalendarForNewEvents.source
</code></pre>

<p>如果你想使用它的源,还要确保默认日历的 <code>allowsContentModifications</code> 属性返回 <code>true</code> 否则你很可能无法创建一个新日历中的事件。 </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 创建新的 EKCalendar iOS 的适当情况,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/39584928/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/39584928/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 创建新的 EKCalendar iOS 的适当情况