gpt4 book ai didi

ios - 检查日历中是否存在事件 - Swift

转载 作者:可可西里 更新时间:2023-11-01 00:35:27 24 4
gpt4 key购买 nike

我正在尝试检查事件是否已存在于日历中并显示它存在的警报。这是我下面的代码

已编辑:它不断添加重复事件。

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")
}

最佳答案

问题是,即使事件存在,您添加事件的 do-catch block 也会一直被调用。您需要创建一个 bool 值来跟踪是否找到了相同的事件,并且仅在未找到时才执行 do-catch block 。

您可以使用 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")
}
}

关于ios - 检查日历中是否存在事件 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45674235/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com