是否有可以从Swift中的日历中删除所有事件的命令?
答案 0 :(得分:9)
考虑到您使用 startDate
, endDate
, description
保存活动, title
var event:EKEvent = EKEvent(eventStore: eventStore)
event.title = "Test Title"
event.startDate = NSDate()
event.endDate = NSDate()
event.notes = "This is a note"
event.calendar = eventStore.defaultCalendarForNewEvents
eventStore.saveEvent(event, span: EKSpanThisEvent, error: nil)
然后您需要做的就是删除事件:
var startDate=NSDate().dateByAddingTimeInterval(-60*60*24)
var endDate=NSDate().dateByAddingTimeInterval(60*60*24*3)
var predicate2 = eventStore.predicateForEventsWithStartDate(startDate, endDate: endDate, calendars: nil)
println("startDate:\(startDate) endDate:\(endDate)")
var eV = eventStore.eventsMatchingPredicate(predicate2) as [EKEvent]!
if eV != nil {
for i in eV {
println("Title \(i.title)" )
println("stareDate: \(i.startDate)" )
println("endDate: \(i.endDate)" )
do{
(try eventStore.removeEvent(i, span: EKSpan.ThisEvent, commit: true))
}
catch let error {
}
}
}
}
答案 1 :(得分:4)
兼容Swift 3 :
func removeAllEventsMatchingPredicate() {
let startDate = NSDate().addingTimeInterval(60*60*24*(-2))
let endDate = NSDate().addingTimeInterval(60*60*24*7)
let predicate2 = eventStore.predicateForEvents(withStart: startDate as Date, end: endDate as Date, calendars: nil)
print("startDate:\(startDate) endDate:\(endDate)")
let eV = eventStore.events(matching: predicate2) as [EKEvent]!
if eV != nil {
for i in eV! {
do{
(try eventStore.remove(i, span: EKSpan.thisEvent, commit: true))
}
catch let error {
print("Error removing events: ", error)
}
}
}
}