正在从 Swift 中的日历中删除提醒

Deleting reminders from calendar in Swift

我使用以下函数检索我的日历:

func retrieveCalendar() -> EKCalendar? {

    appDelegate = UIApplication.sharedApplication().delegate
        as? AppDelegate

    var myCalendar:  EKCalendar?
    let calendars = appDelegate!.eventStore!.calendarsForEntityType(EKEntityTypeReminder) as! [EKCalendar]
    let filteredCalendars = calendars.filter {[=11=].title == "MedicalCalendar"}
    if filteredCalendars.isEmpty {
        println("could not find reminder calendar 'MedicalCalendar'")
        return nil
    } else {
        myCalendar = filteredCalendars[0]
        return myCalendar!
    }

}

但是,每当我向日历添加新事件时,我都想检查它们是否已经存在。我发现最简单的方法是删除所有提醒并重新加载新提醒。我试过了:

self.retrieveCalendar()?.reset()

但是不行。如何从日历中删除提醒?(一次一个或一次全部)

要查看提醒,您必须结合 predicateForRemindersInCalendarspredicateForIncompleteRemindersWithDueDateStarting:ending:calendars:predicateForCompletedRemindersWithCompletionDateStarting:ending:calendars:

调用方法 fetchRemindersMatchingPredicate()

例如,如果你想删除过去到现在所有过期的提醒,使用类似这样的东西

  • 假定属性:

    var calendar : EKCalendar // current calendar
    let eventStore : EKEventStore // current event store
    
  • 代码

    func removeExpiredReminders() {
    
         let pastPredicate = eventStore.predicateForIncompleteRemindersWithDueDateStarting(nil, ending:NSDate(), calendars:[calendar])
    
        eventStore.fetchRemindersMatchingPredicate(pastPredicate) { foundReminders in
    
            let remindersToDelete = !foundReminders.isEmpty
            for reminder in foundReminders  as! [EKReminder] {
                self.eventStore.removeReminder(reminder, commit: false, error: nil)
            }
            if remindersToDelete {
                self.eventStore.commit(nil)
            }
        }
    }
    

在循环中你可以检查进一步的条件