简体   繁体   English

如何从默认日历中删除所有事件

[英]How to delete all events from the default calendar

How can I delete all events in the default calendar when a button is tapped? 轻按按钮时,如何删除默认日历中的所有事件?

This is my current code 这是我当前的代码

     @IBAction func deleteEvents(_ sender: UIButton) {

         eventStore.calendars(for: .event).removeAll()
      }

The first problem it's not the current calendar. 第一个问题不是当前日历。 The second problem is that I get the following error: 第二个问题是出现以下错误:

Error: 错误:

Cannot use mutating member on immutable value: function call returns immutable value 无法对不可变值使用变异成员:函数调用返回不可变值

There is no removeAll method. 没有removeAll方法。

You need to use the methods of EKEventStore to query and remove the desired events. 您需要使用EKEventStore的方法来查询和删除所需的事件。

At a high level you need to: 在较高级别,您需要:

  1. Request authorization to access calendar events. 请求授权以访问日历事件。
  2. Get a reference to desired the EKCalendar . 获取所需的EKCalendar的参考。
  3. Create a predicate for the events you wish to query. 为您要查询的事件创建一个谓词。 You need a date range and the calendar. 您需要一个日期范围和日历。
  4. Enumerate the events that match the predicate. 枚举与谓词匹配的事件。
  5. Delete each enumerated event. 删除每个枚举的事件。

All of the needed APIs are in the EKEventStore class. 所有必需的API都在EKEventStore类中。 See its documentation for specifics. 有关详细信息,请参见其文档。

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

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