我正在尝试检查日历中是否已存在事件并显示其存在的警报。这是我的代码
编辑:它不断添加重复事件。
let event = EKEvent(eventStore: eventStore)
var savedEventId : String = ""
event.title = title
event.startDate = (self.dataEvent?.dates?.begin)!
event.endDate = (self.dataEvent?.dates?.end)!
event.calendar = eventStore.defaultCalendarForNewEvents
let predicate = eventStore.predicateForEventsWithStartDate(startDate, endDate: endDate, calendars: nil)
let existingEvents = eventStore.eventsMatchingPredicate(predicate)
for singleEvent in existingEvents {
if singleEvent.title == self.dataEvent?.titleString && singleEvent.startDate == startDate && singleEvent.endDate == endDate {
let alert = UIAlertController(title: "Event Already Exists", message: "Event Already Exists in Calendar", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
}
do {
try eventStore.saveEvent(event, span: .ThisEvent)
savedEventId = event.eventIdentifier
print("Event Added")
let alert = UIAlertController(title: "Event Successfully Added", message: "Event Added to Calendar", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
} catch {
print("Error occurred")
}
答案 0 :(得分:4)
问题是即使事件存在,您在其中添加事件的do-catch
块也会一直被调用。您需要创建一个布尔值来跟踪是否找到相同的事件,并且只有在找不到do-catch
块时才执行。{/ p>
您可以使用Array.contains(where:)
代替使用for循环,如果发现此类事件,则可以提前退出。
var eventAlreadyExists = false
let event = EKEvent(eventStore: eventStore)
var savedEventId : String = ""
event.title = title
event.startDate = (self.dataEvent?.dates?.begin)!
event.endDate = (self.dataEvent?.dates?.end)!
event.calendar = eventStore.defaultCalendarForNewEvents
let predicate = eventStore.predicateForEventsWithStartDate(startDate, endDate: endDate, calendars: nil)
let existingEvents = eventStore.eventsMatchingPredicate(predicate)
let eventAlreadyExists = existingEvents.contains(where: {event in self.dataEvent?.titleString == event.title && event.startDate == startDate && event.endDate = endDate})
// Matching event found, don't add it again, just display alert
if eventAlreadyExists {
let alert = UIAlertController(title: "Event Already Exists", message: "Event Already Exists in Calendar", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
} else {
// Event doesn't exist yet, add it to calendar
do {
try eventStore.saveEvent(event, span: .ThisEvent)
savedEventId = event.eventIdentifier
print("Event Added")
let alert = UIAlertController(title: "Event Successfully Added", message: "Event Added to Calendar", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
} catch {
print("Error occurred")
}
}
答案 1 :(得分:0)
注意:如果您的endDate和startDate相等-则在谓词时间范围内使用,例如eventStore.predicateForEventsWithStartDate中的endDate + 1,startDate +1。过多地,您现有的活动将始终为空